javascript 实现继承

实现继承

1.ES6之前的版本重中实现继承是一件痛苦个事。如下面代码:

function Person() {}
Person.prototype.dance = function(){};
function NinjaTest() {}
NinjaTest.prototype = new Person();
Object.defineProperty(NinjaTest.prototype, "constructor",{
    enumerable: false,
    value:NinjaTest,
    writable:true
});

这里需要记住几点:对所有实例均可访问的方法必须直接添加在构造函数的原型上。如Person构造函数上的dance方法。为了实现继承,我们必须将实例对象衍生的原型设置成“基类”。在上面的代码中,我们将一个新的Person实例对象赋值NinjaTest.prototype。糟糕的是,这回弄乱constructor属性,所以需要通过Object.defineProperty方法进行手动投置。

console.log("--------------------------在ES6中实现继承------------------------");
class Person{
  constructor(name) {
    this.name = name;
  }

  dance() {
    return true;
  }
}

//使用关键字‘extends’实现继承
class NinjaTest extends Person {
  constructor(name, weapon) {
    //使用关键字'super'调用基类构造函数
    super(name);
    this.weapon = weapon;
  }

  wieldWeapon() {
    return true;
  }
}

var person = new Person("Bob");
if (person instanceof Person) {
  console.log("A person's a person.");
}
if (person.dance()) {
  console.log("A person can dance");
}
if (person.name === 'Bob') {
  console.log("We cam call it by name.");
}
if (!(person instanceof NinjaTest)) {
  console.log("but it is not a NinjaTest");
}
if(!("wieldWeapon" in person)) {
  console.log("And it cannot wield a weapon");
}

var ninjaTest = new NinjaTest("Yoshi", "Wakizashi");
if (ninjaTest instanceof NinjaTest) {
  console.log("A ninjaTest's a ninjaTest!");
}
if (ninjaTest.wieldWeapon()) {
  console.log("That can wield a weapon");
}

if (ninjaTest instanceof Person) {
  console.log("but it is also a person.");
}

if (ninjaTest.name === 'Yoshi') {
  console.log("That has a name");
}

if (ninjaTest.dance()) {
  console.log("And enjoys dancing");
}

上述代码中展示了ES6中的继承。使用extends从一个类实现继承:

class NinjaTest extends Person

在上面的代码中,创建Person类,其构造函数对每一个实例对象添加name属性。同时,定义一个所有Person的实例均可访问的dance方法。

class Person{

constructor(name) {

this.name = name;

}

dance() {

return true;

}

}

接着,我们定义一个从Person类继承而来的NinjaTest类。在NinjaTest类上添加weapon属性和wieldWeapon方法:

//使用关键字‘extends’实现继承

class NinjaTest extends Person {

constructor(name, weapon) {

//使用关键字'super'调用基类构造函数

super(name);

this.weapon = weapon;

}

wieldWeapon() {

return true;

}

}

 

衍生类NinjaTest构造函数通过关键字super调用基类Person的构造函数。这与其他基于类的语言是类似的。

创建Person实例,并验证Person类的实例具有name属性和dance方法,但不具有wieldWeapon方法:

var person = new Person("Bob");

if (person instanceof Person) {

console.log("A person's a person.");

}

if (person.dance()) {

console.log("A person can dance");

}

if (person.name === 'Bob') {

console.log("We cam call it by name.");

}

if (!(person instanceof NinjaTest)) {

console.log("but it is not a NinjaTest");

}

if(!("wieldWeapon" in person)) {

console.log("And it cannot wield a weapon");

}

创建一个ninjaTest实例,并验证ninja是类NinjaTest的实例,具有weaponWield方法。由于所有的ninjaTest同时也是类Person的实例,因此,因此,ninjaTest实例也具有name属性和dance方法:

var ninjaTest = new NinjaTest("Yoshi", "Wakizashi");

if (ninjaTest instanceof NinjaTest) {

console.log("A ninjaTest's a ninjaTest!");

}

if (ninjaTest.wieldWeapon()) {

console.log("That can wield a weapon");

}

if (ninjaTest instanceof Person) {

console.log("but it is also a person.");

}

if (ninjaTest.name === 'Yoshi') {

console.log("That has a name");

}

if (ninjaTest.dance()) {

console.log("And enjoys dancing");

}

从上面可知:定义类,通过关键字extends定义类之间的关系。

 

参考《JavaScript忍者秘籍》

猜你喜欢

转载自blog.csdn.net/zhangying1994/article/details/84993768