JavaScript objects and object-oriented

First, the JavaScript basic data types:

                          number (numeric types)

                          string (string type)

                          boolean (Boolean)

                          the null (empty type)

                         undefined (indicating type)

                         object

Second, is a collection of objects (attribute method) includes properties and methods

Third, the object-oriented: Object-oriented programming is just a concept or idea 

                         Object-oriented programming is achieved by means of a prototype method is called

Fourth, the custom objects:

    Syntax: var object name = new Object ();

Copy the code
Flower new new Object = var (); 
    flower.name = "Vinca"; 
    flower.genera = "oleander flowers is a chief"; 
    flower.area = "East Africa, subtropical, tropical and mainland China, Southwest, South etc. "; 
    flower.uses =" ornamental medication or the like "; 
    flower.showName = function () {Alert (this.name);     
} 
    flower.showName ();
Copy the code

  Create objects using literal assignment mode:

Copy the code
Flower = {var 
        name: "Vinca", 
        genera: "oleander flowers is a chief", 
        Area: "Africa, subtropical and tropical mainland China and East China, Southwest, South and other places", 
        uses: "watch or medication etc. ", 
        showname: function () {Alert (this.name);} 
    } 
 flower.showName ();
Copy the code

Fifth, built-in objects: Common built-in objects

                         String (String) objects

                         Date (date) objects

                         Array (Array) Object

                         Boolean (logical) objects

                         Math (arithmetic) objects

                         RegExp object

Six constructors:  

Copy the code
Flower function (name, the genera, Area, uses) { 
        this.name = name; 
       ....... 
        this.showName = function () { 
            Alert (this.name); 
        } 
    } 
   var = new new flower1 Flower ( "Vinca", " Chief oleander flowers are "," Africa, subtropical and tropical mainland China and East China, Southwest, South and other places, "" watch or medication, etc. ") 
    
   flower1.showName (); 
     var = new new Flower2 Flower (" Peony " "Paeoniaceae Paeonia", "China", "watch, food or medicine"); 

     flower2.showName (); 

     var = new new flower3 flower ( "Mandala flower", "Solanaceae Datura" "India, northern China," "ornamental or medicinal"); 

     flower3.showName ();
Copy the code

Seven, four steps calling destructor

          Create a new object

          The scope constructor assigns the new object (this points to the new object)

          The constructor is executed code

          Returns a new object

 Eight, constructor property points to the constructor

         instanceof operator detection target type

 IX prototype object

Copy the code
Example: 
function Flower () { 

    } 
    Flower.prototype.name = "Datura flowers"; 
    Flower.prototype.genera = "Solanaceae Datura"; 
    Flower.prototype.area = "India, northern China"; 
    Flower .prototype.uses = "ornamental or pharmaceutically acceptable"; 
    Flower.prototype.showName = function () { 
        Alert (this.name); 
    } 
    var = new new flower1 Flower (); 
    flower1.showName (); 
    var = new new Flower2 Flower ( ); 
    flower2.showName (); 
    Alert (flower1.showName == flower2.showName);
Copy the code

X. prototype chain: a prototype object is another instance of an object prototype

                     Progressive layers associated prototype object, constitutes an example of a chain with the prototype, the prototype chain is

Copy the code
示例:
      function Humans(){
        this.foot=2;
    }
    Humans.prototype.getFoot=function(){
       return this.foot;
    }
    function Man(){
        this.head=1;
    }
    Man.prototype=new Humans();          //继承了Humans
    Man.prototype.getHead=function(){
        return this.head;
    }
    var man1=new Man();
    alert(man1.getFoot());                          //2
    alert(man1 instanceof Object);          //true     
    alert(man1 instanceof Humans);        //true
    alert(man1 instanceof Man);          //true
Copy the code

XI borrow constructors: Syntax: apply ([thisOjb [, argArray ]]) ( Application of a method of an object, replacing the current object with another object)

                                               call ([thisObj [, arg1 [, arg2 [, [, argN]]]]]) (a method call to an object, another object to replace the current object)

Copy the code
Example: 
   function Humans (name) { 
        this.name = name; 
    } 
    function Man () { 
        Humans.call (the this, "Mary"); // inherited Humans, also passed parameters 
        this.age = 38; // examples of attributes 
    } 
    var = new new man1 Man (); 
    Alert (man1.name); // output Mary 
    Alert (man1.age); // output 38
Copy the code

Twelve, a combination of inheritance: sometimes called pseudo-classical inheritance prototype chain technology portfolio and borrowed a constructor to play both the long one of the inheriting mode 

                             Prototype prototype implementation inheritance chain properties and methods, and to achieve an instance attribute inherited by borrowed constructor

Guess you like

Origin www.cnblogs.com/danxun/p/11003730.html