原型链继承:
//原型对象上的任何类型的值,都不会被实例所重写/覆盖,
//在实例上设置语原型对象上同名属性的值,只会在实例上创建一个同名的本地属性
//但是是引用类型的就会修改
//原型链继承
function Person(name){
this.name='dfdf'
}
Person.setName=function(name){
this.name=name
}
Person.prototype.setName=function(name){
this.name=name
}
Person.prototype.nmae=[{naem:'eeee'}]
function sun(name){
this.name3=name
}
sun.prototype=new Person()
sun.prototype.say=function(){
console.log('wo'+this.name3)
}
var su=new sun('rr')
//su.setName('eee')
su.say()
su.nmae.push({naem:'ee3333333ee'})
var suu=new sun('444')
//构造函数继承
function Person(name){
this.name=['dfdsafds']
}
Person.setName=function(name){
this.name=name
}
Person.prototype.setName=function(name){
this.name=name
}
Person.prototype.nmae=[{naem:'eeee'}]
function sun(name){
this.name3=name
Person.call(this)
}
//sun.prototype=new Person()
sun.prototype.say=function(){
console.log('wo'+this.name3)
}
var su=new sun('rr')
//su.setName('eee')
su.say()
su.nmae=[{naem:'ee3333333ee'}]
var s3uu=new sun('444')
//组合继承
function Person(name){
this.name=['dfdsafds']
}
Person.setName=function(name){
this.name=name
}
Person.prototype.setName=function(name){
this.name=name
}
Person.prototype.nmae=[{naem:'eeee'}]
function Sun(name){
this.name3=name
Person.call(this)
}
Sun.prototype=new Person()
Sun.prototype.constructor=Sun
var su=new Sun('d')
su.nmae=[{naem:'eeee3333'}]
var su2=new Sun('d')
function inheritPrototype(subType, superType){
//原型式继承:浅拷贝superType.prototype对象作为superType.prototype为新对象的原型
// 内部会自带_proto_指向:prototype.\_\_proto\_\_ = superType.prototype;
var prototype = Object.create(superType.prototype);
// subType.prototype.\_\_proto\_\_ = superType.prototype;
subType.prototype = prototype; // 将子类的原型替换为这个原型
prototype.constructor = subType; // 修正原型的构造函数
}
function SuperType(name){
this.name = name;
this.colors = ["red", "blue", "green"];
}
SuperType.prototype.sayName = function(){
alert(this.name);
};
function SubType(name, age){
SuperType.call(this, name);
this.age = age;
}
// 核心:因为是对父类原型的复制,所以不包含父类的构造函数,也就不会调用两次父类的构造函数造成浪费
inheritPrototype(SubType, SuperType);
SubType.prototype.sayAge = function(){
alert(this.age);
}
var nu=new SubType('33',33)
var nu2=new SubType('334',334)