name); this.age=age; } SubType.prototype=object.create(Super
//界说寄生组合模型
function inherPrototype(SubType,SuperType) {
var prototype = object.create(SuperType.prototype);
//create返回一个新的实例,在一个参数时同object()
SubType.prototype = prototype;
prototype.constructor = SubType;
}
function SuperType(name) {
this.name = name;
this.color = [‘red‘,‘blue‘,‘green‘];
}
SuperType.prototype.sayname = function () {
alert(this.name);
}
function SubType(name,age) {
SuperType.call(this,name);
this.age = age;
}
inherPrototype(SubType,SuperType);//创建原型链
SubType.prototype.sayage = function () {
alert(this.age);
}
//object的源码,object和create在传一个参数的时候成果不异,所以我们附上object的源码
function object(o) {
function F() {}
F.prototype = o;
return new F();
}
/*********************************我来分界*****************************************************/
/*********************************我也来分界*****************************************************/
//我们此刻进行优化一下,没须要这样自界说函数
function SuperType(name){
this.name = name;
}
SuperType.prototype.sayname = () => {
alert(this.name);
}
function SubType(name,age) {
SuperType.call(this,name);
this.age = age;
}
SubType.prototype = object.create(SuperType.prototype);
SubType.prototype.constructor = SubType;
SubType.prototype.sayage = () => {
alert(this.age);
}
let sub1 = new SubType("xiaohong",18);//此刻subtype担任了supetype的属性,
let sub2 = new SubType("xiaoming",19);//而且sub1和sub2这两个实例都有本身的感化域空间,不会影响相互
/*****************************我是一条分界线=^=*****************************************/
/*****************************我是又一条分界线=^=***************************************/
//此刻我们用class来写一下,这里用到了extends要领和super要领
class SuperType{
constructor(name){
this.name = name;
}
sayname(){
alert(this.name);
}
}
class SubType extends SuperType{
constructor(name,age){
super(name);
this.age = age;
}
sayage(){
alert(this.age);
}
}
/*用class简单的处地址于,,我不需要写这么多prototype属性,在界说子类的时候就已经完成了*/
let sub1 = new SubType("xiaohong",18);
let sub2 = new SubType("xiaoming",19);
温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/31296.html