Maison > interface Web > js tutoriel > le corps du texte

JavaScript面向对象继承方法

高洛峰
Libérer: 2016-11-28 11:48:54
original
1373 Les gens l'ont consulté

 JavaScript的出现已经将近20多年了,但是对这个预言的褒贬还是众说纷纭。很多人都说JavaScript不能算是面向对象的变成语言。但是JavaScript的类型非常松散,也没有编译器。这样一来给了程序员很大的自由,也带来了一些缺陷。

  虽然JavaScript不算是一门面向对象的语言。但是我们可以模仿着其他语言实现面向对象的方式来实现JavaScript的面向编程。

  下面是JavaScript教程中非常经典的继承方法。

//定义一个Pet对象。通过这一个名称和数量的腿。  
var Pet = function  (name,legs) { 
    this.name = name; //Save ths name and legs values.  
    this.legs = legs; 
}; 
  
//创建一个方法,显示了Pet的名字和数量的腿。  
Pet.prototype.getDetails = function  () { 
    return this.name + " has " + this.legs + " legs "; 
} 
  
//定义一个Cat对象,继承从Pet。  
var Cat = function  (name) { 
    Pet.call(this,name,4); //调用这个父对象的构造函数  
}; 
  
//这条线执行继承从Pet。  
Cat.prototype = new Pet(); 
  
//增加一个动作方法的猫  
Cat.prototype.action = function  () { 
    return "Catch a bird"; 
}; 
  
//创建一个实例petCat的猫。  
var petCat = new Cat("felix"); 
  
var details = petCat.getDetails();  
console.log(details)                //"felix has 4 legs".   
var action = petCat.action();       
console.log(action)             //"Catch a bird".  
petCat.name = "sylvester";              //改变petCat的名字  
petCat.legs = 7;                //改变petCat腿的数量  
details = petCat.getDetails();      
console.log(details)                //"sylvester has 7 legs". 
 
//定义一个Pet对象。通过这一个名称和数量的腿。
var Pet = function  (name,legs) {
 this.name = name; //Save ths name and legs values.
 this.legs = legs;
};
 
//创建一个方法,显示了Pet的名字和数量的腿。
Pet.prototype.getDetails = function  () {
 return this.name + " has " + this.legs + " legs ";
}
 
//定义一个Cat对象,继承从Pet。
var Cat = function  (name) {
 Pet.call(this,name,4); //调用这个父对象的构造函数
};
 
//这条线执行继承从Pet。
Cat.prototype = new Pet();
 
//增加一个动作方法的猫
Cat.prototype.action = function  () {
 return "Catch a bird";
};
 
//创建一个实例petCat的猫。
var petCat = new Cat("felix");
 
var details = petCat.getDetails();
console.log(details)    //"felix has 4 legs".
var action = petCat.action();   
console.log(action)    //"Catch a bird".
petCat.name = "sylvester";          //改变petCat的名字
petCat.legs = 7;    //改变petCat腿的数量
details = petCat.getDetails();   
console.log(details)    //"sylvester has 7 legs".
Copier après la connexion

  上述方法虽然执行起来没有太大的问题,但是代码整体风格略显臃肿,并不很优雅。在外面还是可以对属性进行修改。这种方法没有对继承的属性进行保护。下面一种方法,省去的new和prototype,利用“函数继承”的特性实现。

//定义一个pet对象。通过这一个名称和数量的腿。  
var pet = function (name,legs) { 
    //创建一个对象that,其中名字是可以改的,但是腿数不可以改,实现了变量私有化。  
    var that = { 
            name : name, 
            getDetails : function  () { 
                return that.name + " has " + legs + " legs "; 
            } 
        }; 
  
    return that; 
} 
  
//定义一个cat对象,继承从pet。  
var cat = function  (name) { 
    var that = pet(name,4); //从pet中继承属性  
  
    //cat中增加一个action的方法。  
    that.action = function  () { 
        return "Catch a bird"; 
    } 
  
    return that; 
  
} 
  
//创建一个petCat2;  
var petCat2 = cat("Felix"); 
  
var details = petCat2.getDetails();  
console.log(details)                //"felix has 4 legs".  
var action = petCat2.action();       
console.log(action)             //"Catch a bird".  
petCat2.name = "sylvester";             //我们可以改变名字。  
petCat2.legs = 7;               //但是不可以改变腿的数量  
details = petCat2.getDetails();     
console.log(details)                //"sylvester has 4 legs". 
 
//定义一个pet对象。通过这一个名称和数量的腿。
var pet = function (name,legs) {
 //创建一个对象that,其中名字是可以改的,但是腿数不可以改,实现了变量私有化。
 var that = {
   name : name,
   getDetails : function  () {
    return that.name + " has " + legs + " legs ";
   }
  };
 
 return that;
}
 
//定义一个cat对象,继承从pet。
var cat = function  (name) {
 var that = pet(name,4); //从pet中继承属性
 
 //cat中增加一个action的方法。
 that.action = function  () {
  return "Catch a bird";
 }
 
 return that;
 
}
 
//创建一个petCat2;
var petCat2 = cat("Felix");
 
var details = petCat2.getDetails();
console.log(details)    //"felix has 4 legs".
var action = petCat2.action();  
console.log(action)    //"Catch a bird".
petCat2.name = "sylvester";          //我们可以改变名字。
petCat2.legs = 7;    //但是不可以改变腿的数量
details = petCat2.getDetails();   
console.log(details)    //"sylvester has 4 legs".
Copier après la connexion

        温馨提示:使用原型继承的好处是内存效率高,不管它被继承多少次,对象的原型属性和方法只被保存一次。函数继承的时候,每个新的实例都会创建重复的属性和方法。若创建很多大的对象,内存消耗会很大。解决方法是把较大的属性或方法保存在一个对象中,并将其作为参数传给构造函数。这样所有实例就会使用一个对象资源,而不是创建自己的版本了。


  上面两种方法都可以轻松实现JavaScript面向对象的继承,没有哪种方法绝对的好,也没有哪种方法绝对的不好。依个人情况喜好而定。


Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!