我试图在javascript中实现继承.我想出了以下最小代码来支持它.
function Base(){
this.call = function(handler, args){
handler.call(this, args);
}
}
Base.extend = function(child, parent){
parent.apply(child);
child.base = new parent;
child.base.child = child;
}
Run Code Online (Sandbox Code Playgroud)
专家,请告诉我这是否足够或我可能错过的任何其他重要问题.根据面临的类似问题,请提出其他更改建议.
这是完整的测试脚本:
function Base(){
this.call = function(handler, args){
handler.call(this, args);
}
this.superalert = function(){
alert('tst');
}
}
Base.extend = function(child, parent){
parent.apply(child);
child.base = new parent;
child.base.child = child;
}
function Child(){
Base.extend(this, Base);
this.width = 20;
this.height = 15;
this.a = ['s',''];
this.alert = function(){
alert(this.a.length);
alert(this.height);
}
}
function Child1(){
Base.extend(this, Child);
this.depth = …Run Code Online (Sandbox Code Playgroud) 我已经阅读了以下QAs,并且所有这些QAs都使用原型继承来模拟经典继承.
在野外没有一个原型继承的工作实例吗?模拟生命形式,也许?除了编程语言创建或未充分解决的问题之外,哪些问题会从原始的原型继承中受益?
我想知道为什么three.js代码的结构如下:
THREE.Camera = function(){
THREE.Object3D.call(this);
//add more Camera specific properties and methods
}
THREE.Camera.prototype = new THREE.Object3D();
THREE.Camera.prototype.constructor = THREE.Camera;
THREE.Camera.prototype.//add more camera specific methods...
Run Code Online (Sandbox Code Playgroud)
我想知道他们为什么在当前构造函数中调用基础构造函数以及原型?
在MDN中,它们显示如下模式:
subType = function(){
//new properties for subType
}
subType.prototype = new baseType();
Run Code Online (Sandbox Code Playgroud)
它们没有在subType构造函数中调用基础构造函数,那么为什么THREE.js会这样做呢?