function Base() {
if (!(this instanceof Base)) {
return new Base();
}
}
function Base() {
if (!(this instanceof Base)) {
return new Base();
}
//开始成员变量定义
this.className = "Base";
}
Base.prototype.printClassName = function(){
console.log(this.className);
}
var base = Base(); //不使用new操作符,直接进行函数调用,自动调用new操作符 console.log(base.className); base.printClassName();
var inherits = function(ctor, superCtor) {
//严格相等测试:undefined/null
//子类构造函数必须存在
if (ctor === undefined || ctor === null)
throw new TypeError('The constructor to "inherits" must not be ' +
'null or undefined');
//严格相等测试:undefined/null
//父类构造函数必须存在
if (superCtor === undefined || superCtor === null)
throw new TypeError('The super constructor to "inherits" must not ' +
'be null or undefined');
//要点: 如果要继承的话,父类必须要有prototype对象
//这也是为什么将所有成员方法都定义在prototype对象中!!!
if (superCtor.prototype === undefined)
throw new TypeError('The super constructor to "inherits" must ' +
'have a prototype');
//让子类构造函数对象增加一个super_指针,指向父类,这样就形成继承链
ctor.super_ = superCtor;
//调用Object.setPrototypeOf(子类的prototype,父类的prototype)
Object.setPrototypeOf(ctor.prototype, superCtor.prototype);
};
// 仅适用于Chrome和FireFox,在IE中不工作:
Object.setPrototypeOf = Object.setPrototypeOf || function (obj, proto) {
obj.__proto__ = proto;
return obj;
}
function Child() {
//老样子,套路1
if (!(this instanceof Child)) {
return new Child();
}
}
//注意,inherits调用不在构造函数,也不在原型对象,而是全局调用 inherits(Child, Base);
function Child() {
//老样子,套路1
if (!(this instanceof Child)) {
return new Child();
}
//增加这句话,在调用printClassName就能正常的输出Base字符串
Base.call(this);
//如果要更新基类的成员变量,请在Base.call(this)之后!
this._className = "Child"; //调用printClassName就能正常的输出Child字符串
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有