Object (two)

How to create an object:

1. Use the native reference type Object()  

 

// Create new Object() with reference Object 
    var obj1= new Object();
    obj1.name="obj1";
    obj1.num =1 ;

 

 

2. Object literal creation

var obj2 = {
        name:"obj2",
        num:2
 };

Disadvantages of these two methods : When many objects need to be created, a lot of repetitive code will be generated

 

3. The factory pattern uses functions to encapsulate the creation of objects with specific interfaces

function obj3(name,num){
         var b=new Object(); //The displayed reference Object creates an object
        b.name=name;
        b.num=num;
        b.sayName=function(){
            alert(this.name);
        };   
        return b; //return an object
 }     
   var people=obj3("obj3",3); // instead of using new, call the function directly and return an object

  The obj3 function can be called countless times, each time returning an object containing two properties and one method;

Disadvantages : cannot solve the problem of object recognition;

 

4. Constructor pattern (create custom constructor)

// Constructor mode
    function Person(name,num){
        this.name=name;
        this.num=num;
        this.sayname=function(){
            alert(this.name);
        }
    }
    var person1=new Person("obj4",4);

  Different from the factory mode: 1. Create objects without display;

                                       2. Directly assign properties and methods to this;

                                       3. There is no return statement;

The instance person1 saves an attribute constructor==Person (equal to the constructor), and the constructor attribute of the object is originally used to identify the object; this is where the constructor pattern is better than the factory pattern; 
disadvantages: each method must be

5. Prototype mode: The constructor has a prototype property that points to the prototype object, and this prototype object contains all the properties and methods shared by the instances created by the constructor; all
prototype objects have a constructor property , pointing to the constructor
// prototype mode
    function Person5(){}; //Custom constructor is empty
    Person5.prototype={
constructor:Person, name:"china", num:5, sayname:function(){ alert(this.name); } }; var obj5 = new Person5 ();

 Disadvantage: Omit passing parameters for the constructor, as a result, all instances will get the same attribute value by default. For reference type value attributes, as long as one instance pushes the value into an array, other instances will also get this new value. value, which is why the prototype pattern is not used alone;

 

6. Combination of constructor and prototype mode Constructor mode is used to define instance properties; prototype mode is used to define methods and shared properties;

//Combined use of constructor and prototype mode
    function Person6(name,num){
       // prototype:Person.prototype;
        this.name=name;
        this.num=num;
    }
    Person6.prototype={
        constructor:Person6,
        sayname:function(){
            alert(this.name);
        }
    }
    var obj6=new Person6("obj6",6);

  

Replenish:

1. Detection object type: instanceof

     Instance instancsof constructor //true

 

2. Check whether the prototype of the instance has an object

      Only the object pointed to by the instance's prototype property returns true; prototype.isPrototypeOf(instance)

       Person6.prototype.isPrototypeOf(obj6)     //true

       Object.isPrototypeOf(obj6)     //false

3. Object.getPrototypeOf()      identifies the prototype

alert(Object.getPrototypeOf(obj6)==Person6.prototype);    //true
alert(Object.getPrototypeOf(obj6)==Object);               //false

  

4. hasOwnProperty() detects whether a property exists in the instance or the prototype. Only if it exists in the object instance will it return true

  name must be enclosed in quotes, otherwise false;

obj6.name=7;
console.log(obj6.hasOwnProperty("name"));     //true  

  

5. Get all properties regardless of whether it is Object.getOwnPropertyNames()

     console.log(Object.getOwnPropertyNames(obj6));
     console.log(Object.getOwnPropertyNames(Person6.prototype));

  

     

 



 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324644454&siteId=291194637