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?

asked May 22, 2018 at 3:23
5
  • 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? Commented May 22, 2018 at 3:27
  • Possible duplicate of Get array of object's keys Commented May 22, 2018 at 3: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?" Commented May 22, 2018 at 3: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? Commented May 22, 2018 at 3:37
  • 1
    Yes there is, sir. https://javascript.info is current and designed for n00bs. I glanced at what you are studying and I got dizzy o_O. Commented May 22, 2018 at 3:42

2 Answers 2

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.

answered May 22, 2018 at 3:29
Sign up to request clarification or add additional context in comments.

2 Comments

Maybe I was just overcomplicated when trying to understand the syntax..
JavaScript is confusing sometimes. :)
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'

answered May 22, 2018 at 3:32

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.