The constructor uses and advantages and disadvantages

Each object has a constructor property above (in the strict sense, is the prototype, the object is found by looking to prototype constructor property). Talked about behind the prototype, I will explain schematically the way.

 1         function CreateObj(uName) {
 2             this.userName = uName;
 3             this.showUserName = function () {
 4                 return this.userName;
 5             }
 6         }
 7         var obj1 = new CreateObj('ghostwu');
 8         var obj2 = new CreateObj('卫庄');
 9         console.log( obj1.constructor === CreateObj ); //true
10         console.log( obj2.constructor === CreateObj ); //true

By default, equal to the object constructor function to instantiate an object constructor, the first constructor is used to identify the role the object, but not particularly accurate, since the constructor be modified,

Identify objects for general use instanceof keyword.

What is instanceof?

To understand this key, we need to figure out the prototype chain here, I put him out in advance.

// assumed instanceof operator is left L, the right is R & lt 
L instanceof R & lt 
 // when instanceof operator, on whether there R.prototype prototype chain L is determined by 
L .__ proto __.__ proto__ ..... === R.prototype ?
// Returns true if there is false otherwise

Note: L recursively find the prototype chain when the instanceof operator, namely L .__ proto __.__ proto __.__ proto __.__ proto __... until you find a date or find the top floor.

So a word to understand the rules instanceof operation is as follows:

instanceof detecting the prototype chain __proto__ the left, to the right of the presence or absence prototype prototype.

 console.log( obj1 instanceof Object ); //true
 console.log( obj2 instanceof Object ); //true
 console.log( obj1 instanceof CreateObj ); //true
 console.log( obj2 instanceof CreateObj ); //true

obj1, obj2 reason is an instance of Object, because all objects inherit from Object

Borrowing Constructor

An empty object, can borrow existing constructor, properties and methods of copying completed.

function CreateObj(uName) {
            this.userName = uName;
            this.showUserName = function () {
                return this.userName;
            }
        }
        var obj = new Object();
        CreateObj.call( obj, 'ghostwu' );
        console.log( obj.userName ); //ghostwu
        console.log( obj.showUserName() ); //ghostwu

Advantages and disadvantages constructor

Instanceof advantage is able to identify objects, a drawback is that each instantiation object are copied over the properties and methods

1         var obj1 = new CreateObj('ghostwu');
2         var obj2 = new CreateObj('卫庄');
3 
4         console.log( obj1.showUserName === obj2.showUserName ); //false

From the above results of the implementation, it can be seen obj1.showUserName obj.showUserName and not in the same [js, the reference type is the address comparison function is a reference type], but the presence of two different
memory addresses, as each attribute objects are not the same, this is no problem, but the code is the same method of execution, there is no need to copy, there are more than a waste of memory. that's shortcomings

The method of how to solve the problem copy constructor multiple times?

function CreateObj(uName) {
            this.userName = uName;
            this.showUserName = showUserName;
        }
        function showUserName (){
            return this.userName;
        }
        var obj1 = new CreateObj('ghostwu');
        var obj2 = new CreateObj('卫庄');
        console.log( obj1.showUserName === obj2.showUserName ); //true

The method of the object point to the same global function showUserName, solves the problem of multiple copies, but the overall function is very easy to be covered, that is, we often say that pollute the global variable.

Better solution?

By prototype (the prototype) the object, the method of writing on a prototype object constructor

function CreateObj(uName) {
            this.userName = uName;
        }
        CreateObj.prototype.showUserName = function(){
            return this.userName;
        }
        var obj1 = new CreateObj('ghostwu');
        var obj2 = new CreateObj('卫庄');
        console.log( obj1.showUserName === obj2.showUserName );      //true

 

Guess you like

Origin www.cnblogs.com/art-poet/p/12112450.html