ECMAScript 6 (ES6) is the next generation standard for the JavaScript language. Because the current version of ES6 was released in 2015, it is also called ECMAScript 2015. In other words, ES6 is ES2015. This article focuses on one of the new content of ES2015: Object.assign. The development of ECMAScript is also introduced at the end of the article.

Object.assign

The object. assign method is used to copy the values of all enumerable properties from one or more source objects to the target Object. It will return the target object.

grammar

Object.assign(target, ... sources)Copy the code
  • Parameters:
    • Target: indicates the target object.
    • Sources: source object.
  • Return value: target object.
  • Description: If an attribute in the target object has the same key, the attribute is overwritten by the attribute in the source. The attributes of the later source will similarly override the earlier attributes.

Pay attention to

The object. assign method copies only the enumerable properties of the source Object itself to the target Object.

The getter methods of the source object are called and the target object is copied using setter methods on the target object. Therefore, it assigns attributes, rather than just copying or defining new attributes.

If the merge source contains getters, this may make it unsuitable to merge the new properties into the stereotype.

  • Both String and Symbol attributes are copied.

  • In the event of an error, for example, TypeError is raised if the property is not writable, and the target object can be changed if any properties are added before the error is raised.

  • Object.assign skips source objects whose value is null or undefined.

The instance

  • Copy an object

    var obj = {name:"devpoint"};
    var copy = Object.assign({}, obj);
    console.log(copy); //{name:"devpoint"}
    Copy the code
  • Deep copy problem

    For deep copies, you need to use other methods. Object. Assign copies property values. If the attribute value of the source object is a reference to the object, it copies only that reference value.

    function test() {
        let obj1 = { name: "devpoint1", address: { city: "Shenzhen1" } };
        let obj2 = Object.assign({}, obj1);
        console.log(JSON.stringify(obj2)); // {"name":"devpoint1","address":{"city":"Shenzhen1"}}
    
        obj1.name = "devpoint2";
        console.log(JSON.stringify(obj1)); // {"name":"devpoint2","address":{"city":"Shenzhen1"}}
        console.log(JSON.stringify(obj2)); // {"name":"devpoint1","address":{"city":"Shenzhen1"}}
    
        obj2.name = "devpoint3";
        console.log(JSON.stringify(obj1)); // {"name":"devpoint2","address":{"city":"Shenzhen1"}}
        console.log(JSON.stringify(obj2)); // {"name":"devpoint3","address":{"city":"Shenzhen1"}}
    
        obj2.address.city = "Shenzhen3";
        console.log(JSON.stringify(obj1)); // {"name":"devpoint2","address":{"city":"Shenzhen3"}}
        console.log(JSON.stringify(obj2)); // {"name":"devpoint3","address":{"city":"Shenzhen3"}}
    
        // Deep Clone
        obj1 = { name: "devpoint1", address: { city: "Shenzhen1" } };
        let obj3 = JSON.parse(JSON.stringify(obj1));
        obj1.name = "devpoint4";
        obj1.address.city = "Shenzhen4";
        console.log(JSON.stringify(obj3)); // {"name":"devpoint1","address":{"city":"Shenzhen1"}}
    }
    test();
    Copy the code
  • Ignore null and undefined

JavaScript’s object.assign () method ignores null and undefined when copying objects. Look at the code listed below:

const obj1 = {
    title: "devpoint",
};

const obj2 = Object.assign({}, obj1, null, undefined, { city: "Shenzhen" });
console.log(obj2); // { title: 'devpoint', city: 'Shenzhen' }
Copy the code

ECMAScript profile

Say first JavaScript

In 1994, Netscape released version 0.9 of its Navigator browser. It was the first full-fledged web browser in history, and it was a hit. However, this version of the browser can only be used for browsing and does not have the ability to interact with visitors. For example, if a web page asks for a “user name,” the browser can’t tell if the visitor actually filled it in, leaving it to the server. If not, the server returns an error and asks the user to fill it out again, which is a waste of time and server resources.

So Netscape desperately needed a web scripting language that could be embedded in web pages so that browsers could interact with them. What is a Web scripting language? Netscape had two options, both of which had their pros and cons. Netscape’s management was struggling to make up its mind:

  • Take existing languages, such as Perl, Python, Tcl, Scheme, and so on, and allow them to be embedded directly into web pages;
    • Advantages: make full use of existing code and programmer resources, easy to promote
  • Invent a whole new language
    • Advantages: It is easy to develop a fully applicable language

That’s when another big thing happened: Sun introduced the Oak language to the market in 1995, renamed Java.

With Sun’s hype that it could “Write Once, Run Anywhere,” it seemed likely to dominate the future. Netscape was moved and decided to form an alliance with Sun. Not only does it allow Java programs to run directly in the browser as applets; I even considered embedding Java directly into web pages as a scripting language, only to abandon it later because it would have made HTML pages too complex. Anyway, the situation was that the entire management of Netscape was Java believers, and Sun was completely involved in the web scripting language decisions. As a result, Javascript was later brought to market by netscape and Sun, and it was no accident that the language was named “Java+ Script “.

In April 1995, Netscape hired Brendan Eich, a 34-year-old systems programmer. Brendan Eich’s primary interest and interest is functional programming, and netscape recruited him to investigate the possibility of using the Scheme language as a web scripting language. Brendan Eich himself felt the same way, assuming that he would be working primarily with Scheme when he arrived at his new company. But just a month later, in May 1995, Netscape decided that the web scripting language of the future would have to “look similar enough to Java” but be simpler enough to make it accessible to non-professional web authors. This decision effectively excludes non-object-oriented programming languages such as Perl, Python, Tcl, and Scheme. Brendan Eich was designated as the designer of this “simplified Version of the Java language.”

However, he has no interest in Java at all. He designed Javascript in just 10 days to meet the company’s demands. Because the design time was too short, some of the details of the language were not carefully considered, resulting in a long time later, Javascript written programs chaotic.

In general, his design idea goes like this:

  • Learn from the basic syntax of C language;
  • Using the Java language for data types and memory management;
  • Using Scheme language to elevate functions to the status of “first class”;
  • Reference Self language, using prototype – based inheritance mechanism.

So the Javascript language is really a hybrid of the two language styles —- (simplified) functional programming + (simplified) object-oriented programming. This was decided by Brendan Eich (functional programming) and Netscape (object-oriented programming).

Brendan Eich designed the scripting language for The Netscape Navigator 2.0 browser that Netscape was preparing for release in 1995. The language was originally called LiveScript, but Netscape changed its name to JavaScript just before the official release of Netscape Navigator 2.0, in an effort to capitalize on Java’s Internet buzz word. This is JavaScript version 1.0. The version was launched to great success.

Because JavaScript 1.0 was so successful, Netscape released version 1.1 in Netscape Navigator 3.0. Around that time, Microsoft decided to move into the browser, releasing IE 3.0 with a JavaScript clone called JScript (so named to avoid potential licensing disputes with Netscape). Microsoft’s infamous step into the Web browser space was also an important step in the development of the JavaScript language.

After Microsoft moved in, there were three different versions of JavaScript that existed at the same time:

  • JavaScript in Netscape Navigator 3.0
  • JScript in IE
  • ScriptEase in CEnvi (a scripting language for embedding web pages developed by a company called Nombas).

Unlike C and other programming languages, JavaScript does not have a standard to unify its syntax or features, and these three different versions highlight this problem. As industry concerns grow, it is clear that standardization of the language is inevitable.

The birth of ECMAScript

In November 1996, Netscape, the creator of JavaScript, decided to submit JavaScript to the international standardization organization ECMA, hoping that the language would become an international standard. ECMA stands for European Computer Manufacturers Association. ECMA is an international standards organization for information transfer and communication.

ECMA’s Technical Committee 39 (TC39), which writes the ECMAScript standard, includes Netscape, Sun, Microsoft, Mozilla, Google and other large companies.

In 1997, ECMA released the first version of the standard document 262 (ECMA-262), which specified the standard for the browser scripting language and called it ECMAScript. This version became version 1.0.

The standard was written for the JavaScript language from the beginning, but it’s not called JavaScript for two reasons.

  • Java is a trademark of Sun Corporation. Under the license agreement, only Netscape can legally use the name JavaScript, and JavaScript itself has been registered as a trademark by Netscape corporation.

  • ECMA, not Netscape, was the creator of the language, which helped keep the language open and neutral.

Thus, the relationship between ECMAScript and JavaScript is that the former is a specification of the latter and the latter is an implementation of the former. Jscript and ActionScript are also implementations of ECMAScript.

The development course

  • In 1997, ECMAScript 1.0 was released.
  • In 1998, ECMAScript 2.0 was released
  • In 1999, ECMAScript 3.0 was released. Version 3.0 was a huge success, gaining widespread support in the industry and becoming the prevailing standard, laying down the basic syntax of the JavaScript language, which later versions inherited completely. To this day, when beginners start learning JavaScript, they are learning the syntax of version 3.0.
  • In 2000, ECMAScript 4.0 was hatched. This version did not pass. Why didn’t ES4 pass? Because this version was so radical, it was a radical upgrade to ES3 that some members of the standards committee were reluctant to accept it.
  • In 2007, a draft version of ECMAScript 4.0 was released, with a formal release expected in August of the following year. However, the parties are deeply divided over whether to adopt this standard. Big companies, led by Yahoo, Microsoft, and Google, oppose big upgrades to JavaScript in favor of small changes. Mozilla, led by JavaScript creator Brendan Eich, is sticking with the current draft.
  • In 2008, there was so much disagreement and debate over what features should be included in version 4.0 that ECMA met and decided to discontinue development of ECMAScript 4.0 and release it as ECMAScript 3.1 (shortly after the meeting, ECMAScript 3.1 was renamed ECMAScript 5), and other radical ideas expanded into later versions. Due to the atmosphere of the conference, the 4.0 project was codenamed Harmony.
  • In 2009, ECMAScript version 5.0 was released. The Harmony project was split in two, and some of the more viable ideas continued to be developed as javascripts. Next, which later evolved into ECMAScript 6; Some less mature ideas are seen as javascript.Next-next, to be rolled out further in the future.
  • In 2011, ECMAscript 5.1 was released and became an ISO international standard.
  • In March 2013, ECMAScript 6 draft was frozen without adding new features.
  • In December 2013, a draft of ECMAScript 6 was released.
  • In June 2015, ECMAScript 6 was officially approved and became an international standard. Fifteen years had passed since 2000.

ECMAScript 6 introduces a number of new features, focusing on strengthening modules, class declarations, lexical block delimiting, iterators and generators, callbacks for asynchronous programming, schema resolution, and appropriate tail calls, as well as extending the ECMAScript built-in library to support more abstract data structures. The goal is to make JavaScript an enterprise-level development language that can be used to write complex applications.


After the