JavaScript based on prototype object-oriented system __java

Source: Internet
Author: User

We know that everything in JavaScript (reference type) is an object, an object is a collection of attributes, but how does an object in JavaScript actually create it? In Java, an object is created by instantiating the class, and the object is always created from the class, and in JavaScript there is no class (the class in ES6 is just a syntactic sugar). JavaScript is a prototype object-oriented language, in the prototype programming idea, the class is not necessary, the object may not need to be created from the class, an object is by cloning another object. Important thing to say three times: an object is obtained by cloning another object, which is obtained by cloning another object, and the object is obtained by cloning another object.

The JavaScript prototype pattern is a pattern for creating objects, first finding an object, and then cloning to create an identical object. ECMASCRIPT5 provides a object.create () method that can be used to clone objects.

Let person = function () {
  this.name = ' Shanshui ' this.age = ' ~ '} let person
= new Person ()
PERSON.N Ame = ' Zinong '
person.age = ' let
Cloneperson = object.create (person)
Console.log (cloneperson.name)// ' Zinong '
console.log (cloneperson.age)//' 20 '

Cloneperson is done by cloning a person object. But how is the person object created? In the JavaScript language, we do not need the details of the relationship cloning, because this is responsible for the internal implementation of the engine, all we need to do is to show the call let Obj1 = new Object () or let obj2 = {}. At this point, the engine will clone an object from the Object.prototype, and we end up with this object.

Let someone = new person () creates a person object from the person constructor using the new operator, and in the process of creating the object with the new operator, In fact, it's just a process of cloning object.prototype objects and doing some other extra work. Let's look at what new did when the let person = new person ().

var obj = new Object ()
obj.__proto__ = Person.prototype
person.call (obj)

The new approach is to create an object obj, and let obj's __proto__ point to the Person.prototype object and use call for a strong-turn environment, enabling the creation of the instance.

The implementation code for Object.create is as follows, Object.create internally defines an F constructor and lets the F.prototype object assign value to the imported object/function proto and return a new object. The objects returned by the Object.create clone are also created using the new operator.

if (typeof object.create!== "function") {object.create = function (proto, Propertiesobject)  {if (typeof Proto!== ' object ' && typeof proto ' function ') {!== new throw (' Object
        Prototype May is an Object: ' + Proto '; else if (proto = = null) {throw new Error ("This browser's implementation of Object.create is a shim and doe

        SN ' t support ' null ' as the ' argument ');} if (typeof propertiesobject!= ' undefined ') throw new Error ("This browser ' s implementation of Object.create's is a shim and

        Doesn ' t support a second argument. ");

        function F () {} f.prototype = proto;
    return new F ();
}; }
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.