###介绍es6
function Parent(){
this.name="Parent"
}
Parent.prototype.smoking=function(){
console.log("抽烟")
}
let parent =new Parent()
console.log(parent.__proto__)//指向所属类的原型
console.log(parent.__proto__.constructor)//指向当前所属类的
console.log(parent.__proto__.__proto__) //指向Object
console.log(Parent.__proto__)//Function
console.log(Parent.__proto__.__proto__)//Object
复制代码
function Parent(){
this.name="Parent"
}
function Child(){
this.age="18";
Parent.call(this)
}
let child = new Child()
child.name;// "Parent"
复制代码
function Parent(){
this.name="Parent"
}
Parent.prototype.smoking=function(){
console.log("抽烟")
}
function Child(){
this.age="18"
}
Child.prototype.eat=function(){
console.log("吃雪糕")
}
let child= new Child()
//一、将子类的原型的__proto__指向父类的 Prototype
Child.prototype.__proto__ = Parent.prototype
// 错误写法 Child.prototype = Parent.prototype 不能是父亲原型等于孩子原型 这样孩子原型会被覆盖 至关于不存在了 再取child.eat会报错
//二、es6写法
Child.prototype = Object.create(Parent.prototype,{constructor:{value:prototype}})
//3)、es7
Object.setPrototypeof(Child.prototype,Parent.prototype)
复制代码
class Parent{
constructor(name){
this.name =name
}
say(){
console.log("say")
}
}
class Child extends Parent{
}
let child = new Child("儿子")
console.log(child.name);// 儿子
child.say();// "say"
复制代码