function Person() { //被继承的函数叫做超类型(父类,基类)
this.name='mumu';
this.age='18';
}
Person.prototype.name='susu';//当属性名相同时需就近原则,先在实例里面查找,没找到再到原型里找
function Worker(){ //继承的函数叫做子类型(子类,派生类)
this.job='student';
}
Worker.prototype=new Person();//通过原型链继承,超类型实例化后的对象实例,赋值给子类的原型属性
var p2=new Worker();
console.log(p2.name);
console.log(p2 instanceof Object);//ture 所有的构造函数都继承自Object
function Person() {
this.bodys=['eye','foot'];
}
function Worker(){
}
Worker.prototype=new Person();
var p1=new Worker();
p1.bodys.push('hand');
var p2=new Worker();
console.log(p1.bodys);
console.log(p2.bodys);
function Person(name,age){
this.name=name;
this.age=age;
this.bodys=['eye','foot'];
}
Person.prototype.showName=function(){
console.log(this.name);
}
function Worker(name,age,job){
Person.call(this,name,age);
this.job=job;//子类添加属性
}
var p1=new Worker('mumu','18','学生');
p1.bodys.push('hand') ;
var p2=new Worker();
console.log(p1.name);
console.log(p2.bodys);
console.log(p1.showName());
function Person(name,age){
this.name=name;
this.age=age;
}
Person.prototype.showName=function(){
console.log(this.name);
}
function Worker(name,age,job){
Person.call(this,name,age);//借用构造函数
this.job=job;
}
Worker.prototype=new Person();//原型链继承
var p1=new Worker('mumu','18','学生');
console.log(p1.age);
p1.showName();
function object(proto) {
function F() {}
F.prototype = proto;
return new F();
}
var person = {
name: 'mumu',
friends: ['xiaxia', 'susu']
};
var anotherPerson = object(person);
anotherPerson.friends.push('wen');
var yetAnotherPerson = object(person);
anotherPerson.friends.push('tian');
console.log(person.friends);//["xiaxia", "susu", "wen", "tian"]
console.log(anotherPerson.__proto__)//Object {name: "mumu", friends: Array[4]}
//临时中转函数
function object(proto) {
function F() {}
F.prototype = proto;
return new F();
}
//寄生函数
function create(proto){
var f=object(proto);
f.love=function(){
return this.name;
}
return f;
}
var person = {
name: 'mumu',
friends: ['xiaxia', 'susu']
};
var anotherPerson = create(person);
console.log(anotherPerson.love());寄生组合式继承
function object(proto) {
function F() {}
F.prototype = proto;
return new F();
}
//寄生函数
function create(Person,Worker){
var f=object(Person.prototype);//创建对象
f.constructor=Worker;//调整原型构造指针,增强对象
Worker.prototype=f;//指定对象
}
function Person(name,age){
this.name=name;
this.age=age;
}
Person.prototype.showName=function(){
console.log(this.name);
}
function Worker(name,age,job){
Person.call(this,name,age);
this.job=job;
}
create(Person,Worker);//寄生组合式继承
var p1=new Person('mumu','18','学生');
p1.showName();
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有