[英]How to call parent class' method from a subclass in JavaScript so that parent's local variables would be accessible?
我正在使用一種方法在JavaScript中進行類繼承(如我正在修改的代碼中使用的方法),但不了解如何將子類中方法的其他功能附加到各自父類方法已具有的功能上; 換句話說,我想用一個方法重寫子類中的父方法,該方法除了其自己的特定於子類的東西外,還執行與父方法相同的方法。 所以,我試圖從孩子的方法中調用父母的方法,但是有可能嗎?
代碼在這里: http : //jsfiddle.net/7zMnW/ 。 請打開開發控制台以查看輸出。
代碼也在這里:
function MakeAsSubclass (parent, child)
{
child.prototype = new parent; // No constructor arguments possible at this point.
child.prototype.baseClass = parent.prototype.constructor;
child.prototype.constructor = child;
child.prototype.parent = child.prototype; // For the 2nd way of calling MethodB.
}
function Parent (inVar)
{
var parentVar = inVar;
this.MethodA = function () {console.log("Parent's MethodA sees parent's local variable:", parentVar);};
this.MethodB = function () {console.log("Parent's MethodB doesn't see parent's local variable:", parentVar);};
}
function Child (inVar)
{
Child.prototype.baseClass.apply(this, arguments);
this.MethodB = function ()
{
console.log("Child's method start");
Child.prototype.MethodB.apply(this, arguments); // 1st way
this.parent.MethodB.apply(this, arguments); // 2 2nd way
console.log("Child's method end");
};
}
MakeAsSubclass(Parent, Child);
var child = new Child(7);
child.MethodA();
child.MethodB();
不,您看不到父母的局部變量。 您繼承父代原型鏈,而不繼承其本地狀態。 在您的情況下,您要將父函數應用於不持有狀態的子對象。
apply(this,...)
表示您正在將該函數綁定到此函數的當前值。 當從子對象調用方法b時,該方法隨后綁定到子對象,因此不在包含父值的閉包內操作。
我會建議再次使用私有實例值屬性,因為這樣會弄亂原型。 需要訪問私有實例變量(每個實例都有其私有值)的函數不能放在原型上,因此您無法真正使用它。
這是您的代碼如何工作的方式(但我自己不會這樣做):
var Parent = function(){
var private=22;
this.showPrivate=function(){
console.log(private);
}
}
var Child=function(){Parent.call(this)};
// the following is only usefull if you have functions
// on the parent prototype that don't use privates
Child.prototype=Object.create(Parent.prototype);
// Child.prototype.constructor would now point to Parent
// it's not needed most of the time but you can fix that
Child.prototype.constructor=Child;
var c = new Child();
c.showPrivate();
這是使用“私有”功能的方法:
var Parent = function(name){
//public instance variable
this.name=name;
}
Parent.prototype=function(){
// privates on the prototype (shared among instances)
var privateFunction=function(me){
console.log("private function called in:"+me.name);
console.log("shared in "+me.name
+" is now:"+shared+" setting it to 'changed'");
shared="Changed";
}
// private shared value
var shared="Parent"
return{
constructor:Parent,
publicFn:function(){
privateFunction(this);
}
};
}();
var Child=function(name){Parent.call(this,name)};
Child.prototype=Object.create(Parent.prototype);
Child.prototype.constructor=Child;
var c = new Child("child1");
var c2 = new Child("child2");
c.publicFn();
c2.publicFn();//shared is already changed by 'child1'
此處有關構造函數的更多信息
變量var parentVar = inVar;
是一個局部變量和私有變量,僅在Parent()
函數中可用,並且僅對在同一作用域( Parent()
)中定義的函數可見。
我認為最好的方法是修改Parent類,使parentVar
不是私有的,而是公開的:
function Parent(inVar) {
this.parentVar = inVar;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.