function obj2(x,y){
this.name=x,
this.age=y
} 创建一个构造函数(构造方法用来产生对象)
obj2.prototype.getName=function(){return this.name}
//在obj2的原型链上加上一个方法getName
//基础知识:ES5只有函数有prototype
let OBJ2 = new obj2('jack',22)//通过new方法创造一个实例对象OBJ2
class obj3{
constructor(){
this.name='tom',
this.age=23
}
getName(){
return this.name
}
}//通过ES6CLASS方法创造一个对象,并写一个方法
let OBJ3 = new obj3()
// console.log(obj1)
// console.log(obj2.prototype)
// console.log(obj3.prototype)
// console.log(OBJ2)
// console.log(OBJ3)
//ES5与ES6通过构造函数生成新对象的区别
//ES5的继承(原型链继承)
function obj4(){
this.address='changsha'
}
obj4.prototype = new obj2('fuck',24)
console.log(obj4.prototype)
let OBJ4 = new obj4()
//ES6的继承
class obj5 extends obj3{
constructor(){
super();
this.address='hunan'
}
}
let OBJ5 = new obj5()
console.log(OBJ5)
ES5的继承与ES6的继承
最新推荐文章于 2023-07-11 19:10:10 发布