1.继承方式
function animal(){}
animal.prototype.hi=function(){
console.log('welcome to animal world')
}
2.第二种,实例方法,(new animal()实例原型会指向animal.prototype)
cat.prototype=new animal();
3.第三种cat.prototype的原型会指向Object.create的第一个参数
cat.prototype=Object.create(animal.prototype)
4.考虑兼容问题,所以模拟了一个Object.create方法
if(!Object.create){
Object.create=function(proto){
function F(){}
F.prototype=proto;
return new F;
}
}
var cat1=new cat();
function animal(){}
animal.prototype.hi=function(){
console.log('welcome to animal world')
}
function cat(){}
1.第一种(不可取,修改cat会影响到animal)
cat.prototype=animal.prototype;2.第二种,实例方法,(new animal()实例原型会指向animal.prototype)
cat.prototype=new animal();
3.第三种cat.prototype的原型会指向Object.create的第一个参数
cat.prototype=Object.create(animal.prototype)
4.考虑兼容问题,所以模拟了一个Object.create方法
if(!Object.create){
Object.create=function(proto){
function F(){}
F.prototype=proto;
return new F;
}
}
var cat1=new cat();
cat1.hi();
暂时就到这里,后期会有更新