Javascript - Private Members Explanation?
Solution 1:
I think you've mis-interpreted that bit of information. It doesnt say that private methods are those that are "passed through" the constructor, it says its those that are "made by" the constructor.
To be clear, look at this:
functioncar(brand) {
var year = 2012;
var color = "Red";
}
That has 3 private variables. brand
,year
and color
. By adding this line
this.brand = brand
You are creating a public property and assigning it the value from your private variable. That you've named the public property and the private variable the same thing is neither here nor there, if it makes it clearer think of it as
this.publicBrand = brand
Solution 2:
It's not that you can access values passed into the constructor. What you've done is set this.brand
equal to the value passed in the constructor. Therefore, the publicly available brand
now has the same value that was passed in. The local brand
inside the constructor != this.brand
until you set it.
Solution 3:
Everything you assign to the context (this inside function) is public available. Be aware that the context is the window object if you call the function without new
"use strict"functioncar(brand) {
this.brand = brand; //Public (can be accessed from outside because it is attached to the context/this)var year = 2012; //Private (inside function only)var color = "Red"; //Private (inside function only)
}
var bmw = newcar("BMW");
console.log(bmw.brand); // BMW -> VISIBLE -> this.brand = brans
Solution 4:
Answer : Using closures to create a inaccessable "private" scope.
"use strict";
(function (parent) {
(function (parent) {
var instances = {};
parent.getPrivateInstance = function (c) {
return instances[c];
};
parent.setPrivateInstance = function (c, value) {
instances[c] = value;
};
} (this));
parent.Class = function (name) {
setPrivateInstance(this, {
name: name
});
};
parent.Class.prototype.logName = function () {
console.log(getPrivateInstance(this).name);
};
})(window);
var c = newClass("test");
c.logName(); // "test"console.log(c.name); // undefined
Caution: Memory leak
This will create a situation wherein the garbage collector will no longer clear the memory associated with the instances of Class
because it they will always be referenced, which results in a memory leak.
To combat this we'll have to manually remove the reference made to the instances of Class
. This can be done by adding one piece of code after the parent.setPrivateInstance
section and one piece of code after the parent.Class.prototype.logName
section. Those pieces of code would look like this:
parent.deleteFromMemory = function (c) {
delete instances[c];
};
parent.Class.prototype.deleteFromMemory = function () {
deleteFromMemory(c);
};
Usage
c = c.deleteFromMemory();
For a example of all the pieces working together: https://jsfiddle.net/gjtc7ea3/
Disclaimer
As this solution causes a memory leak I would personally advice against using it unless you know what you are doing, as it is very easy to make mistakes here.
Post a Comment for "Javascript - Private Members Explanation?"