[JavaScript Grammar Learning] A comprehensive introduction to objects

Source: Internet
Author: User
Tags iterable shallow copy

How to determine the property of an object

1. In

2. hasOwnProperty ()

The attribute (key name) of the object in JS must be a string, in order to compensate for the difference with other languages (the key name can be any type)

The introduction of map and set in the latest ES6 standard

The iterable type is introduced in the ES6 standard, and the Array Map set belongs to the iterable type, and you can use the For...of loop to traverse

The For...of loop loops only the elements of the collection itself

ES5.1 Standard ForEach

ForEach (element, index, array)

ES6 extension to Object

1. Concise Representation of attributes

Allows you to write directly to variables and functions as properties and methods of an object, write only the property name so that the property value is equal to the variable with the same Name property.

var foo = "Bar"; var baz = {    Foo}; // equivalent to {Foo:foo} function f (x, y) {    return  {x, y};} // equivalent to return {x:x, y:y}

2. The method can be abbreviated

var o = {    method () {        return "Hello, es6";    }}

3. ES6 allows expressions to be used as property names in object literal definitions

The expression of the property name and the concise notation of the property name cannot be used simultaneously

4. Object.is () to compare whether two values are strictly equal, consistent with the = = = operator

But with two exceptions, we know that Nan and Nan are not equal in ES5, but that is true with Object.is (Nan, Nan).

+0 and 0 are equal, but using object.is (+0,-0) is false

5. Object.assign () is used to copy all the enumerable properties of the source object onto the target object, and the properties of the subsequent object override the properties of the previous object for the property with the same name.

Copies only its own properties. Non-enumerable and inherited properties are not copied

This method is only shallow copy, requires deep copy can use _.defaultsdeep (Lodash library)

If the method is applied to an array, the value of the member index of the array is looked at as the property name.

Object.assign ([2], [4,5])//  get [4,5,3]//  [All-in-a-1] as {"0": 1, ""): 2, "": 3}< /c4>//  [4, 5] as {"0": 4, "1": 5}

Application scenario: Simplifying the way you add properties and methods to objects

object.assign (Someclass.prototype, {    somemethod (arg1, arg2) {        }    Anothermethod () {        }}); // add a method to the prototype of an object class

Cloning objects

Merging multiple objects

Specifying a default value for a property

6. In ES5, the description of the object property can be obtained through the Getownpropertydescriptor () method

ES5, there are three operations that omit the non-enumerable property (enumerable = False)

For ... in

Object.keys ()

Json.stringify ()

In ES6, two new actions are ignored for non-enumerable properties

Object.assign ()

Reflect.enumerate ()

ES6 explicitly stipulates that all class prototype methods are non-enumerable

7. Property traversal of an object

ES6 There are 6 ways to traverse object properties

A. for...in all enumerable properties that traverse the object itself and inherit

B. Object.keys (obj) returns an array that contains all the enumerable properties of the object itself

C. object.getownpropertynames (obj) returns an array containing all the properties of the object itself (contains non-enumerable properties)

D. Object.getownpropertysymbols (obj) returns an array containing all the symbol attributes of the object itself

E. Reflect.ownkeys (obj) returns an array containing all the properties of the object itself (contains the symbol attribute, non-enumerable property)

F. Reflect.enumerate (obj) returns a iterator object that iterates through the object itself and inherits all the enumerable properties that are identical to for...in

The order in which the object properties are traversed follows the following rules

1. Iterate through all attributes that have property names as numeric values, sort by numbers

2. Traverse all attributes with property names as strings, sorted by build time

3. Traverse all attributes named symbol values, sorted by build time

8. Extension Operators for objects

The rest parameter of the object, when the assignment is deconstructed, assigns all the objects of the object itself to the specified object, and all the keys and values are copied to the new object. Note that this copy is also a shallow assignment.

If a key value is a composite type, then only the reference to that value is copied.

An extension operator for an object is any traversed property that is used to take out a parameter object and then copied into the current object. Similar to Object.assign ()

[JavaScript Grammar Learning] A comprehensive introduction to objects

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.