function MathHandle(x,y){
this.x = x
this.y = y
}
MathHandle.prototype.add = function(){
return this.x + this.y
}
var m = new MathHandle(1,2)
console.log(m.add())
class MathHandle{
constructor(x,y){
this.x = x
this.y = y
}
add(){
return this.x + this.y
}
}
const m = new MathHandle(1,2)
console.log(m.add())
console.log(typeof MathHandle) // 'function'
console.log(MathHandle.prototype.constructor === MathHandle) //true
console.log(m.__proto__ === MathHandle.prototype) //true
//动物
function Animal(){
this.eat = function (){
console.log('Animal eat')
}
}
//狗
function Dog() {
this.bark = function (){
console.log('Dog bark')
}
}
Dog.prototype = new Animal()
var hashiqi = new Dog()
hashiqi.bark()
hashiqi.eat()
class Animal {
constructor(name){
this.name = name
}
eat(){
alert(this.name + ' eat')
}
}
class Dog extends Animal {
constructor(name){
super(name) //super就是被继承的对象的constructer
}
say(){
alert(this.name + ' say')
}
}
const dog = new Dog('哈士奇')
dog.say()
dog.eat()
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |