Dynamic prototype model, the model configured parasitic - JS Object Oriented

Dynamic prototype model

Combination constructors model and prototype model that OO programmers are confused when they see an independent constructors and prototypes. Dynamic prototype model to address this problem, it is all the information encapsulated in the constructor, by the initialization in the constructor prototype (only if necessary), while using the constructor and advantages of the prototype.

Example code:

<! DOCTYPE HTML> 
<HTML> 
<head> 
    <title> Dynamic prototype model </ title> 
    <Script type = "text / JavaScript"> // Dynamic prototype model: all the information encapsulated in the constructor. By the initialization in the constructor prototype (only in necessary), while maintaining the advantages of using both constructors and prototype model.    function Student (name, Age, Sex) {            
             the this .name = name;
             the this .age = Age;
             the this .sex = Sex;
             the this .friends = [ "Kitty", "Court" ];    
             IF ( typeof the this .sayName =! "function" ) {
                 // can not be used to rewrite the prototype object literal 
                Student.
        
         function(){
                alert(this.name);
                };
            }
        }

        var stu1=new Student("Lucy",10,"girl");
        stu1.sayName();
        var stu2=new Student("Bob",9,"boy");
        stu2.sayName();        
        stu1.friends.push("Van");
        alert(stu1.friends);//"Kitty,Court,Van"
        alert(stu2.friends);//"Kitty,Court"
        alert(stu1.friends===stu2.friends);//false
        alert(stu1.sayName===stu2.sayName);//true
    </script>
</head>
<body>
</body>
</html>

Parasitic model constructor

The basic idea: to create a function that role is merely to create the object code for the package, and then returns the newly created object.

The following code illustrates the basic idea of ​​the model:

<!DOCTYPE html>
<html>
<head>
    <title>寄生构造函数模型</title>
    <script type="text/javascript">            function Student(name,age,sex){    
            var  o=new Object();    
            o.name=name;
            o.age=age;
            o.sex=sex;
            o.friends=["Kitty","Court"];    
            o.sayName=function(){
                alert(this.name);
                };return o;where
        }
        
            

         stu1=new Student("Lucy",10,"girl");
        stu1.sayName();
        alert(stu1 instanceof Student);    //false
        alert(stu1 instanceof Object);//true
    </script>
</head>
<body>
</body>
</html>

Note: There is no relationship between the properties of the prototype object constructor or a constructor returned; namely: object constructor returns to the object outside constructor creates no different. Instanceof operator can not rely on determined object types.

This mode is used to create a constructor for an object in exceptional cases.

 

Guess you like

Origin www.cnblogs.com/planetwithpig/p/11531122.html