最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

oop - How to override private variable in javascript? - Stack Overflow

programmeradmin2浏览0评论
// 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".

How my understanding is wrong?

Thanks for any help.

// 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".

How my understanding is wrong?

Thanks for any help.

Share Improve this question edited Jun 28, 2010 at 15:46 Péter Török 116k31 gold badges277 silver badges331 bronze badges asked Sep 17, 2009 at 9:27 rajakvkrajakvk 10.2k17 gold badges49 silver badges51 bronze badges 7
  • 2 It won't fix your problem, but I think you need to declare aMan before using it as the prototype for Indian. – Andy Commented Sep 17, 2009 at 9:35
  • 6 This wins the "odd code samples of the day"-award, hands down. :) – Tomalak Commented Sep 17, 2009 at 9:35
  • 2 @Andrew: He doesn't need to declare it (var is not an inline statement, vars are always treated as though they appear at the top of the scope), but he does need to initialize it. But you're right, that's not the basic problem, the pattern itself is the problem. – T.J. Crowder Commented Sep 17, 2009 at 9:41
  • @Tomalak Thanks for that award. :-)) – rajakvk Commented Sep 17, 2009 at 9:59
  • 1 Javascript doesn't really have private variables because it isn't really OO. What you're really describing is variables across different scopes. It really is worth learning the difference as it will make understanding problems like this a lot easier. Douglas Crockford has written an excellent book on this: oreilly./catalog/9780596517748 – Keith Commented Sep 17, 2009 at 12:41
 |  Show 2 more ments

3 Answers 3

Reset to default 3

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.
Also you should have set the ctor to Indian.
And at last, getLover is a closure that refers to Man and not to 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. The lover local variable inside Man is the one in-scope for that function. 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.

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.

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.

发布评论

评论列表(0)

  1. 暂无评论