简体   繁体   中英

JavaScript Object Syntax & Notation

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.

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!

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:

在此处输入图像描述

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM