Javascript arrays?

This may be completely stupid, but if I type this into my browser console:

var arr = []; arr.item = 'val'; console.log( arr ); arr; 

What is arr; syntax arr; performing backstage? I assume console.log( arr ); iterates through all the properties of the arr object, but what does arr; do arr; ? Also, [] tell me that I am dealing with an object of an array of types, and {} tells me that I am dealing with an object of type object literal? So, [ item: 'val' ] is an object with an array prototype, and { item: 'val' } is an object literal prototype object?

Edit: Another way to ask this might be why console.log and arr; different?

+5
source share
2 answers

What is arr; syntax arr; performing backstage?

This is an ExpressionStatement expression. The JavaScript engine tries to evaluate an expression by reading the value of a variable and then does nothing with it. If it is present inside the javascript function, it will not do anything (except when arr not a variable, this will cause an error). If you just write arr; in the developer console, most browsers will echo the values ​​of the last expression entered, so as a result, you will see the arr value. The echo mechanism is often implemented similarly to console.log ;

I assume console.log( arr ); iterates over all properties of an arr object

console.log displays the value in the developer console. If the value is a string, the string is printed. If the value is an object, then it depends on the browser what is happening. Most browsers, such as Chrome, will display an interactive representation of the object, for example:

enter image description here

Another browser may simply call toString on an object that will actually iterate over the elements and join them separated by commas.

Also [] tell me that I am dealing with an object of an array of types, and {} tells me that I am dealing with an object of type object literal?

Yes and yes. [1, 2, 3] is an array literal that will evaluate a new instance of an array containing these values. [] is just an empty array. Arrays are instances of type Array . {a:1,b:2} is an object literal, each time it will be evaluated by a new instance. {} is an empty borderless object. Object literals evaluate to instances of type Object .

So, [ item: 'val' ] is an object with an array prototype, and { item: 'val' } is an object literal prototype object?

No and yes. [ item: 'val' ] is not valid syntax. Array literals have elements separated by commas; they cannot have a colon in them.

 [ item: 'val' ] VM154:1 Uncaught SyntaxError: Unexpected token :(…) 

: - this is an unexpected token, the expression cannot be evaluated, because it has syntax errors.

On the other hand, { item:'val' } is a fully regular object literal of type Object and will have one field named item with the value 'val' .

About object properties

In javascript, an object does not have a fixed set of properties. Properties can be dynamically added and deleted by writing them and using the delete keyword. Each object has field (or symbol) names. An array can be thought of as an object optimized for whole keys. But still, the keys are strings, so a[0] means the same as a["0"] .

 var arr = [1, 2]; arr.item = 'val'; // assigns a new field to the object, called item arr['item'] = 'val'; // does exactly the same as the previous arr[1] = 3; // sets the second item in arr to 3 arr['1'] = 3; // does exactly the same as the previous console.log(arr); // prints [1, 2, item: "val"] 

Thus, arbitrary named fields can be assigned in an array, and when printing Chrome notes that the object is an array, therefore it uses array notation, but also prints β€œother” assigned values. It is important to note that not every field is printed, since arr.length is a fully functional property. That's how the guys from Google decided to print arrays. This is not valid JavaScript syntax , it is just a (pretty good) visualization of the data inside the array.

About classes and prototypes

We say that foo has a class foo if foo has a prototype Foo.prototype , in other words foo.__proto__ === Foo.prototype . Here foo is an object, foo is a constructor function. Since foo itself is a function, Foo.__proto__ === Function.prototype .

If subtypes are involved, then we can say that bar is an instance of foo , if bar has Foo.prototype in the prototype chain, i.e.: bar.__proto__ === Foo.prototype || bar.__proto__.__proto__ === Foo.prototype || ... bar.__proto__ === Foo.prototype || bar.__proto__.__proto__ === Foo.prototype || ... bar.__proto__ === Foo.prototype || bar.__proto__.__proto__ === Foo.prototype || ... Explicit access to the .__proto__ object is considered bad practice and is very discouraged. Changing the __proto__ value results in serious performance __proto__ because it violates browser optimization strategies. So how to check if an object is a type? You can use instanceof operator.

+2
source

Sorry, I started to enter a comment and realized that this is really a complete answer.
console.log gives a string representation depending on the implementation of the argument. It does not iterate over the properties of the object in most cases that I have seen. String serialization using the toString method looks like [object Foo] by default, where "Foo" is an internal property of the object class. You can see this for arrays using Object.prototype.toString.call([]); .

The last line, on the other hand, is just a value. How this is perceived as an evaluated expression in your browser console depends on the implementation, but for all purposes and tasks it does nothing, and there is no reason why you would like to write it.

Note that adding a property to an array using the operator . almost never what you want: it does not add to the contents of the array, but treats the array as an object. Iterating over the attributes of an array does not give this property, and its methods, such as .map , do not include it. Use .push . If you need properties with string keys, use an object.

+3
source

Source: https://habr.com/ru/post/1237963/


All Articles