Does it really matter that instances have the same function, but at the same time have private variables?

I have this piece of code:

var Human=function(name){ this._name=name; }; Human.prototype.Shout=function(){ alert(this._name); }; var tom=new Human("tom"); var john=new Human("john"); alert(tom.Shout===john.Shout); 

Right now ._name is not "private". I want to make ._name "private", but at the same time I don’t want to create additional functions for each instance of Human (in other words tom.Shout should be === for john.Shout), since it creates additional functions for each case, it’s just good .. unnecessary (ok offtopic - we can discuss this in another topic)

My conclusion is that what I am trying to achieve (with the name ._name be "private" and at the same time with tom.Shout===john.Shout ) is not possible.

But I just want to be 200% more confident before entering into any conclusions.

(I welcome any hacks subject to compliance with the requirements, i.e. the creation of additional functions for each instance)

If we need to create additional functions in order to make this appear, but this number should be a fixed number, and this number should not increase with each additional instance of Human.

+6
source share
3 answers

Update

Your search @name , which is an instance variable. Pray for it on es.next, but we don’t have it yet. Maybe in two years.

If you need a clean API, here is your solution:

 function Class(foo) { Class.priv(this).foo = foo; } Class.priv = (function() { var cache = [], uid = 1; return function(obj) { if (!this.__id) { this.__id = uid; cache[uid++] = {}; } return cache[this.__id]; }; }()); Class.prototype.bar = function() { console.log(Class.priv(this).foo); } 

Store all data in the cache as a constructor function. No data clutters the object.

Original

However, there is no such thing as "private".

All you can do is create a local variable inside the function.

Constructor function

 var Human = function(name) { // local variable. var _name = name; } 

It has a local variable, which by the very definition of a local variable cannot be used outside the constructor function.

This means that you cannot access it in external code such as a prototype.

However, you can only do this with ES5

 var Human = function(name) { Object.defineProperty(this, "name", { value: name }); } 

If you can really achieve what you ask for, you will make a huge breakthrough in js. I tried to do this for hours.

Another template:

 var Human = function(name) { this.name = name; return { Shout: this.Shout.bind(this) }; } Human.prototype.Shout = function() { console.log(this.name); } 

This has the overhead of calling .bind and creating a new object for each instance.

+3
source

how about this?

  var Human = function (name) { var _name = name; this.getName = function () { return _name; } }; Human.prototype.Shout = function () { alert(this.getName()); }; var tom = new Human("tom"); var john = new Human("john"); tom.Shout(); // tom john.Shout(); // john alert(tom.Shout === john.Shout); // true 

EDIT: the first creates another function for the GET property, this is not possible without creating additional functions.

+1
source

I read the question, did not understand, because this._name simply not closed, so the question is a bit strange. Here's how prototype methods are added to my test once and are available for all instances. I repeat: this._name is not private here. If you add a local variable and want to access it through closure in the prototype method, calling the value of the local variable will result in the same value for all instances.

In any case, using this constructor function, the this._name getter and shout methods are added to the prototype chain once and, therefore, are available for all instances of Human.

 function Human(name) { if (!(this instanceof Human)){ return new Human(name); } this._name = name; if (!Human.prototype.Name){ Human.prototype.Name = function(val){ if (val){ this._name = val; return this; } return this._name; }; Human.prototype.shout = function(){ alert(this._name); } } } 
+1
source

Source: https://habr.com/ru/post/888081/


All Articles