带保护变量的javascript继承
在javascript中是否可能有一个变量不能访问类的函数,但可以被继承它的类访问?即:带保护变量的javascript继承,javascript,oop,Javascript,Oop,在javascript中是否可能有一个变量不能访问类的函数,但可以被继承它的类访问?即: class1 has protected var x = 4; class2 inherits class1; class2.prototype.getVar = function(){return /* parent, uber, super, whatever */ this.x;}; var cl2 = new class2(); console.log(cl2.x) // undefined
class1 has protected var x = 4;
class2 inherits class1;
class2.prototype.getVar = function(){return /* parent, uber, super, whatever */ this.x;};
var cl2 = new class2();
console.log(cl2.x) // undefined
console.log(cl2.getVar()) // 4
不可以。原型继承仅限于对象的属性 构造函数中的变量仅对该变量范围内的其他代码可用 你可能会想出一些像
function cls1() {
var a = 'foo';
this.some_func = function() {
alert(a);
};
}
function cls2() {
cls1.apply(this, arguments);
var cls1_func = this.some_func;
var b = 'bar'
this.some_func = function() {
cls1_func.apply(this, arguments);
alert(b);
};
}
var x = new cls2;
x.some_func(); // alert "foo" alert "bar"
或者让它更具体到您的伪代码
function class1() {
var x = 4;
this.getVar = function() {
return x;
};
}
function class2() {
class1.apply(this, arguments);
var cls1_get_var = this.getVar;
this.getVar = function() {
return cls1_get_var.apply(this, arguments);
};
}
class2.prototype = Object.create( class1.prototype );
var cl2 = new class2;
console.log(cl2.x) // undefined
console.log(cl2.getVar()) // 4
不可以。原型继承仅限于对象的属性 构造函数中的变量仅对该变量范围内的其他代码可用 你可能会想出一些像
function cls1() {
var a = 'foo';
this.some_func = function() {
alert(a);
};
}
function cls2() {
cls1.apply(this, arguments);
var cls1_func = this.some_func;
var b = 'bar'
this.some_func = function() {
cls1_func.apply(this, arguments);
alert(b);
};
}
var x = new cls2;
x.some_func(); // alert "foo" alert "bar"
或者让它更具体到您的伪代码
function class1() {
var x = 4;
this.getVar = function() {
return x;
};
}
function class2() {
class1.apply(this, arguments);
var cls1_get_var = this.getVar;
this.getVar = function() {
return cls1_get_var.apply(this, arguments);
};
}
class2.prototype = Object.create( class1.prototype );
var cl2 = new class2;
console.log(cl2.x) // undefined
console.log(cl2.getVar()) // 4
我认为你需要用闭包来实现你的目标。大概是这样的:
Class1 = function() {
var x = 4;
return {
getVar: function() {
return x;
}
}
} ();// executes the function immediately and returns an
//an object with one method - getVar. Through closure this method
//still has access to the variable x
Class2 = function() { };// define a constructor function
Class2.prototype = Class1;//have it inherit from Class1
Cl2 = new Class2();//instantiate a new instance of Class2
console.log(Cl2.x);//this is undefined
console.log(Cl2.getVar());//this outputs 4
这是javascript的一个优点,因为您可以在javascript中实现与在基于类的语言中相同的功能,而无需所有额外的关键字。Douglas Crockford(总是很好地参考javascript)解释了原型继承
编辑:
请再看一下您的问题。如果您希望类中新创建的方法访问基类中的变量,则必须在您自己的方法中调用getVar方法。如下所示:
Class2 = function() {
this.getVar2 = function() {
return this.getVar();
}
};
console.log(Cl2.getVar2()) //outputs 4
我认为你需要用闭包来实现你的目标。大概是这样的:
Class1 = function() {
var x = 4;
return {
getVar: function() {
return x;
}
}
} ();// executes the function immediately and returns an
//an object with one method - getVar. Through closure this method
//still has access to the variable x
Class2 = function() { };// define a constructor function
Class2.prototype = Class1;//have it inherit from Class1
Cl2 = new Class2();//instantiate a new instance of Class2
console.log(Cl2.x);//this is undefined
console.log(Cl2.getVar());//this outputs 4
这是javascript的一个优点,因为您可以在javascript中实现与在基于类的语言中相同的功能,而无需所有额外的关键字。Douglas Crockford(总是很好地参考javascript)解释了原型继承
编辑:
请再看一下您的问题。如果您希望类中新创建的方法访问基类中的变量,则必须在您自己的方法中调用getVar方法。如下所示:
Class2 = function() {
this.getVar2 = function() {
return this.getVar();
}
};
console.log(Cl2.getVar2()) //outputs 4
不直接;JavaScript并不是这样设计的,真的不需要它。私有变量和受保护变量的设计只是为了防止程序员伤害自己。“真的没有必要这样做。”是的,你已经说过了——防止程序员伤害自己!也许会有所帮助披露:是我写的,所以我有偏见;)-你必须稍微修改一下语法,但它相当有用;JavaScript并不是这样设计的,真的不需要它。私有变量和受保护变量的设计只是为了防止程序员伤害自己。“真的没有必要这样做。”是的,你已经说过了——防止程序员伤害自己!也许会有所帮助披露:是我写的,所以我有偏见;)-您必须稍微修改一下语法,但它相当有用。