2019-11-04 javascript to achieve the create method

This article refers to https://www.cnblogs.com/chenwenhao/p/11294541.html

1. Background

Use easyui + jquery items as the front frame, before the back-end code is substantially more design, the front end of the application involved only slightly.

Js project is a framework written before the department heads, the use of process-oriented coding method, coupling deeper front-end code, modify them stretched.

We hope that a unified approach to management of design codes jquery, jquery source code is necessary to have a certain level of understanding.

The best way to understand the code, I feel it should be made a simplified version of the wheel. After the prototype chain generally understood herein by reference Bowen create a time function.

 

2. Implement

 

constructor constructor property is automatically assigned to itself after the statement, while the People's __proto__ property assigned to Function.prototype.

Function.call created using new methods and ways of distinguishing objects exist on the prototype.

 

 

 new way point to which the object constructor __proto__ property prototype object, Function.call embodiment the object prototype object point __proto__ property Object.

Thus the Function.call () as long as the way to create an object point to modify __proto__ property, private considered equivalent to create objects in new ways, as follows.

function Create () {
     // 1. Create an empty object 
    the let obj = {};
    
    // 2. Get constructor 
    the let constructor = [] .shift.call (arguments);

    // 3. Link to prototype 
    obj .__ proto__ = constructor.prototype;

    // 4. Binding this value 
    the let Result = constructor.apply (obj, arguments);

    // 5. Return the new object 
    return  typeof the Result === 'Object'? The Result: obj;
}

Called to

var people = create(People,'Bob',22);

 

 

Guess you like

Origin www.cnblogs.com/hexx/p/11792135.html