Trying to learn javascript by reading underscore sourcecode and came across the below code:
var shallowProperty = function(key) {
return function(obj) {
return obj == null ? void 0 : obj[key];
};
};
var getLength = shallowProperty('length');
console.log(getLength('123'))//3
console.log('123'['length'])//3
console.log(getLength(['1','2']))//2
console.log(['1','2']['length'])//2
My question is, what is [length] besides ['1','2']? Any technical terms to call it? Where can we get the full list of keys/attributes available other than length?
asked May 22, 2018 at 3:23
Isaac
13k18 gold badges66 silver badges135 bronze badges
2 Answers 2
An array is a JavaScript object. An object can have properties. You can access them in a few, equivalent ways:
myObject.property
myObject['property']
See this MDN documentation.
To show all the properties of an object:
Object.getOwnPropertyNames(myObject);
You might like to refer to this question about listing the properties of an object.
answered May 22, 2018 at 3:29
Matt
3,7651 gold badge18 silver badges25 bronze badges
Suppose you have the following object:
let person = {
name: 'foo',
age: 23
}
// Then, there are two possible ways to get the properties of "person" object
console.log(person.name); // logs 'foo'
console.log(person['name']); // logs 'foo'
answered May 22, 2018 at 3:32
user9090230
Comments
lang-js
console.log(['1','2']['length'])in the page you hyperlinked. Can you send me a link to the actual page?