[英]How to override private variable in javascript?
// base function
function Man(name) {
// private property
var lover = "simron";
// public property
this.wife = "rocy";
// privileged method
this.getLover = function(){return lover};
// public method
Man.prototype.getWife = function(){return this.wife;};
}
// child function
function Indian(){
var lover = "jothika";
this.wife = "kamala";
}
Indian.prototype = aMan;
var aMan = new Man("raja");
oneIndian = new Indian();
oneIndian.getLover();
I got answer as "simron" but I expect "jothika". 我得到的答复是“ simron”,但我希望“ jothika”。
How my understanding is wrong? 我的理解错了吗?
Thanks for any help. 谢谢你的帮助。
First of all your code doesn't work at all and it's wrong. 首先,您的代码根本不起作用,这是错误的。
Here's the code that works: 这是有效的代码:
// base function
function Man(name) {
// private property
var lover = "simron";
// public property
this.wife = "rocy";
// privileged method
this.getLover = function(){return lover};
// public method
Man.prototype.getWife = function(){return this.wife;};
}
// child function
function Indian(){
var lover = "jothika";
this.wife = "kamala";
this.getLover = function(){return lover};
}
Indian.prototype = new Man();
Indian.prototype.constructor = Indian;
var oneIndian = new Indian();
document.write(oneIndian.getLover());
aMan didn't exist until you declared it. 直到您声明aMan都不存在。
Also you should have set the ctor to Indian. 另外,您应该将ctor设置为Indian。
And at last, getLover is a closure that refers to Man and not to Indian. 最后,getLover是一个闭包,它指向Man而不是Indian。
Declaring it again refers it to the right scope. 再次声明它会指向正确的范围。
See here and here for further details and improvements of your code. 请参见此处和此处,以获取更多详细信息和代码改进。
The getLover
property on the instance refers to the closure you defined within the Man
constructor. 实例的getLover
属性引用您在Man
构造函数中定义的闭包。 The lover
local variable inside Man
is the one in-scope for that function. Man
内的lover
局部变量是该函数的唯一作用域。 The lover
variable you declared inside Indian
has nothing whatsoever to do with the one declared inside Man
, no more than local variables declared inside other functions do. 您在Indian
中声明的lover
变量与在Man
声明的变量没有任何关系,只不过在其他函数中声明的局部变量无关。
For Indian
to manipulate the private lover
variable inside Man
, you would have to give Indian
some access to it via an accessor function -- but then everything would be able to change it via that same accessor function. 为了使Indian
能够在Man
操作私有lover
变量,您必须通过访问器函数为Indian
一些访问权限-但随后,所有内容都可以通过同一访问器函数对其进行更改。
My advice: get rid of this whole priviledged method crap and don't try to shoehorn concepts from one language into another. 我的建议:摆脱所有特权方法的废话,不要试图将概念从一种语言转换为另一种语言。
For performance reasons, methods should reside in the prototype. 出于性能原因,方法应驻留在原型中。 Otherwise, a new function object (which forms a closure over the constructor's local vaiables) has to be created for each instance, which is highly inefficient. 否则,必须为每个实例创建一个新的函数对象(对构造函数的本地变量进行闭包),这是非常低效的。
If you want to hide properties (ie 'private' fields), add a prefix like _private_
to their name and tell the programmer not to do stupid things. 如果要隐藏属性(即“私有”字段),请在其名称前添加诸如_private_
类的前缀,并告诉程序员不要做愚蠢的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.