你真的 会写吗?在不参考的情况下,试试
//1. class 更加简单 上手
//2. class 更加符合 面向对象语言的写法
//3. class 本质还是 语法糖,用的是 prototype
// js 实现拓展方法
function MathHandel(x,y){
this.x = x;
this.y= y;
}
MathHandel.prototype.add = function(){
console.log('MathHandel add func',this.x+this.y);
}
let x = new MathHandel(5,6);
x.add();
// js 实现继承
function Animal(){
this.eat=function(){
console.log('animal can eat ...');
}
}
function Dog(){
this.break=function(){
console.log('dog can break');
}
}
Dog.prototype = new Animal();
let y = new Dog();
y.eat();
y.break();
// class 实现 继承
class Animalgg{
constructor(name){
this.name=name;
}
eat(){
console.log('gg animal can wat 。。。');
}
}
class Dogkk extends Animalgg{
constructor(name){
super(name)
}
break(){
console.log('kk dog can break 。。。');
}
}
let z = new Dogkk('xiao he');
z.eat();
z.break();
网友评论