-1

I've been using JavaScript for a short period of time, and have found myself interested as to whether or not there is any correlation between the . syntax used for referencing children of an object, and functions such as console.log.

And was interested whether functions such as this, were in a way objects behind the scenes, with log being a method inside of a console object, and if I am just referencing the child of an object.

Or another example, the .length method, is this just a hidden property of the object or array you are referencing.

Apologies if this is poorly worded, I was struggling to write down my train of thought, and I would be incredible appreciative if anyone could let me know if this is how methods such as these are structured, or if I am thinking of them in completely the wrong way.

pie
  • 9
  • 2
  • 1
    This is literally how JS works: objects have any number of properties (either as direct property, or as [prototype property](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object_prototypes)), and you access those with [the property accessor syntax](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors). If that property is a function, then you can run it using `(...)`, but this is the very core of how objects in JS work =) – Mike 'Pomax' Kamermans Apr 20 '21 at 18:35
  • 1
    `length` can't be considered "hidden" when you reference it directly by name. –  Apr 20 '21 at 18:42

2 Answers2

2

Notice how you can access the length property on a string. While string is a primitive, the length property actually exits on a wrapper object that is created temporarily when we try to read .length. Arrays and other collections have this property, because they are objects themselves. Hope this helps to clarify a bit!

-1

console.log is not a function, per se. The function is the log property on the console object. console is an object with many methods:

enter image description here

To access it's method, just like to access any property of a JS object, you can use dot notation or square bracket notation

var log = console.log;
log('hello');

var logSquare = console['log'];
logSquare('hello');

// or just do
console['log']('hello');

Except for primitives, everything in JS is an object.

Adam
  • 41,349
  • 10
  • 58
  • 78
  • Note that while `log('hello')` works for the console specifically, it won't work in the general case. – georg Apr 20 '21 at 18:42
  • https://stackoverflow.com/questions/28668759/what-does-this-statement-do-console-log-bindconsole –  Apr 20 '21 at 18:44
  • @georg Yes, I know where you're going, and of course you're correct. But to avoid even further confusion, let's stick to addressing the OPs immediate question – Adam Apr 20 '21 at 18:44
  • @Adam: honestly, I find these examples more confusing than helpful, because that's precisely how things _don't_ work in javascript. – georg Apr 20 '21 at 18:50
  • @georg - you're viewing this answer from a place of knowledge. The OP is not. – Adam Apr 20 '21 at 19:26