Isaac
Isaac

Reputation: 12894

What is array besides string/arrays?

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?

Upvotes: 3

Views: 64

Answers (2)

user9090230
user9090230

Reputation:

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'

Upvotes: 1

Matt
Matt

Reputation: 3760

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.

Upvotes: 1

Related Questions