How to turn to jumps ?

 function Animal(name) { var speed = 10 return { name: name, run: function() { alert(jumps) } } } function Rabbit(name) { var me = Animal(name) var jumps = 0 me.jump = function() { jumps++ } me.getJumps = function() { return jumps } me.constructor = arguments.callee return me } 

var ogj = Rabbit ()

  • 2
    Rabbit (name) .getJumps (); - Gorets
  • I mean property, not method Rabbit (name). jumps - Zow 9:21 pm
  • Are you sure you are writing to JS? And what do you understand what you write? - SoWa
  • This is not my function, javascript.ru/tutorial/object/inheritance - Zow am
  • > Not everything that happens when javascript is inherited, the article describes absolutely correctly. the authors themselves assert their ignorance! and even after that you continued to read it? - Specter

2 answers 2

What is the meaning of a private property, if it is so easy to access it from outside?

Actually the answer to your question is simple to ugliness - no way. Also add - jumps , as it were, not a property, but a variable.

PS: For the sake of fairness I will add - in languages ​​with the classical implementation of the OOP, usually, access to private properties can be obtained using reflation. But the question is not about such a language, besides, it is not even about the property.

  • why then use private variables? - Zow
  • just facepalm - Specter
  • that they could not be changed, like constants only in the object? - Zow

What is this syntactic construction, how should it work according to the idea? There is no way to get to the variable jumps - it exists locally in the Rabbit runtime. If you attributed

 me.jumps = 0; me.jump = function (){return this.jumps++} 

then yes, your variable will be a property of the returned object and it will be accessed.