我知道的JS-继承


继承

继承可以使得子类具有父类的各种属性和方法
在高程中介绍了好多种,但是完美的就这一种,所以,就理解好这一种就可以了

ES5 中的继承

function Human(name) {
  this.name = name;
}
Human.prototype.run = function() {
  console.log("i can run");
};
function Man(name) {
  Human.call(this, name);
  this.gender = "男";
}
//如果 Man.prototype.__proto__ = Human.prototype就实现了继承
// 但是这样写是不允许的,
//所以
Man.prototype = Object.create(Human.prototype);
Man.prototype.constructor = Man;

ES6 中的继承

class Human {
  constructor(name) {
    this.name = name;
  }
  run() {
    console.log("i can run");
  }
}
class Man extends Human {
  constructor(name) {
    super();
    this.gender = "男";
  }
  fight() {
    console.log("fight");
  }
}

文章作者: 沐雪
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 沐雪 !
评论
 上一篇
我知道的JS-函数 我知道的JS-函数
函数函数就是一段可以反复调用的代码块.函数还能接受输入的参数,不同的参数会返回不同的值.具名函数,匿名函数,箭头函数 this & argumentsthis 就是 call 一个函数时,传入的第一个参数(一般是对象)call 的其
2018-04-14
下一篇 
我知道的JS-异步 我知道的JS-异步
异步PromisePromise 是 Es6 新增的语法,为了解决回调地狱的问题Promise 有三种状态,pending(初始状态),可以通过函数 resolve 和 reject 把状态变为 resolved 或者 rejected,状
2018-04-09
  目录