3

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?

Isaac
  • 12,042
  • 16
  • 52
  • 116
  • I can't seem to find `console.log(['1','2']['length'])` in the page you hyperlinked. Can you send me a link to the actual page? – Ibrahim Tareq May 22 '18 at 03:27
  • Possible duplicate of [Get array of object's keys](https://stackoverflow.com/questions/8763125/get-array-of-objects-keys) – Nir Alfasi May 22 '18 at 03:27
  • Why learn JavaScript by studying a one off version of the language? Syntax, methods, properties, etc. might be slightly different or not even exist in JavaScript. You'll start getting confused and ask questions like: *"Any technical terms to call it?"* – zer00ne May 22 '18 at 03:34
  • @zer00ne: Having comments beside every sections of code can really help a total newbie like me very much. Or there is any other better recommendations? – Isaac May 22 '18 at 03:37
  • 1
    Yes there is, sir. [https://javascript.info](https://javascript.info) is current and designed for n00bs. I glanced at what you are studying and I got dizzy o_O. – zer00ne May 22 '18 at 03:42

2 Answers2

1

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.

Matt
  • 3,677
  • 1
  • 14
  • 24
1

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'