ES6 new feature: Object change analysis, es6 new feature object

Source: Internet
Author: User

ES6 new feature: Object change analysis, es6 new feature object

This article describes the Object changes in the new features of ES6. We will share this with you for your reference. The details are as follows:

Object changes

1. ES6 allows you to write only the attribute name in the object without the attribute value. The attribute value is the variable value corresponding to the attribute name.

var a = 'hi';var obj = {a};console.log(obj); //Object {a: "hi"}

2. Short for methods in the object.

var a = 'hi';var obj = {  name: 'ES6',  a,  sayHi(){    console.log(this.a+' '+this.name);  }}obj.sayHi(); //hi ES6

3. ES6 allows the expression to be used as the property name of an object when defining an object literally.

var a = 'b';var obj = {  [a]: 'ES6',  ['c' + 'd']: 'hi'}console.log(obj); // Object {b: "ES6", cd: "hi"}

4. The method name in the object can be accessed through name.

var a = 'hi';var obj = {  name: 'ES6',  a,  sayHi(){    console.log(this.a+' '+this.name);  }}obj.sayHi(); //hi ES6console.log(obj.sayHi.name); //sayHi

5. Object. is ()

It is used to compare whether two values are strictly equal. The difference with = is that Object. is (NaN, NaN) returns true, Object. is (+ 0,-0) returns false.

6. Object. Assign ()

Copies all the enumerated attributes of the source object to the target object.

var obj_source_1 = {  a: {    a1: 'hi',    a2: 'ES6'  },  b: 'hello'}var obj_source_2 = {  c: 'ES2015',}var result = Object.assign({}, obj_source_1, obj_source_2);console.log(result); //Object {a: Object, b: "hello", c: "ES2015"}

General Usage: Add attributes and methods for objects, clone objects, and merge objects.

I hope this article will help you design the ECMAscript program.

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.