Object Oriented JS - hidden in combination with model constructor prototype model

The use of a combination of models and prototypes constructor model, using the object model prototype rewritable literal that is hidden (related to the dynamic nature of the prototype), in the following example:

<! DOCTYPE HTML> 
<HTML> 
<head> 
    <title> in combination of model and prototype model constructor - Problems </ title> 
    <Script type = "text / JavaScript"> // combination of model and prototype model constructor - - constructor model used to define the instance attribute, a prototype model is used to define methods and shared properties.    function Student (name, Age, Sex) {            
             the this .name = name;
             the this .age = Age;
             the this .sex = Sex;
             the this .friends = [ "Kitty", "Court" ];             
        } // This embodiment Error @ STU1 new new Student = var ( "Lucy", 10, "Girl");         // Student.
        
        

        
        
        
        
             sayName: function () { 
        //          Alert (this.name); 
        //      } 
        // }         
        // stu1.sayName (); // use object literal rewriting prototype model error 

        var STU2 = new new Student ( "Bob" ,. 9, "Boy" ); 
        Student.prototype.sayName = function () { 
                Alert ( the this .name); 
            };     
        stu2.sayName ();
     </ Script> 
</ head> 
<body> 
</ body> 
</ html>

In learning dynamic prototype model, found in the book marked "when using dynamic prototype model, you can not use an object literal rewriting prototype.", I think of the combination is to use the object literal rewriting of the prototype model, so to verify whether there is problems, so write a little experience, to avoid future mistakes.

Guess you like

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