javascript 实现继承

本文详细介绍了在ES6中如何使用类和继承特性来简化JavaScript的面向对象编程。通过对比ES6之前复杂的继承实现方式,展示了使用class和extends关键字进行类定义和继承的简洁性和有效性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实现继承

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忍者秘籍》

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值