Answered by:
JavaScript - Getter/Setter in Objects's Prototype is not working. Getter returns last element

Question
-
I am new to JavaScript. While working with given javascript, I'm expecting the output values as "123"; "test1"; "456" and "test2", as there are two instances.
But the returned values are "456";"test2"; "456" and "test2".
If I Change the setter to work on "this" (Which is currently coomented out in code), the problem resolves. I am unable to understand why protype object is behaving diffrently.
I don't want to declare the variables with this, as i want variables to be private, which can be accessed only through getter/setters method.
<html> <head> <script type="text/javascript"> function Test(){ var name; var id; Test.prototype.setId = function(newId){ id = newId; } //this.id = function(newId){ Test.prototype.getId = function(){ return id; } Test.prototype.setName = function(newName){ name = newName; } //this.getName = function(){ Test.prototype.getName = function(newGuid){ return name; } } function callme(){ var instance1 = new Test(); instance1.setId("123"); instance1.setName("test1"); var instance2 = new Test(); instance2.setId("456"); instance2.setName("test2"); alert(instance1.getId()); alert(instance1.getName()); alert(instance2.getId()); alert(instance2.getName()); } </script> </head> <body> <button onclick='callme()'> Test </button> </body> </html>
Thursday, January 24, 2013 3:49 PM
Answers
-
Hi,
You can use the constructor function. And refer to the follow example:
function User(name) { this.name = name; } User.prototype.getName = function() { return this.name; }; var user = new User('Bob');
Roy
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help.- Marked as answer by Song Tian Thursday, January 31, 2013 12:49 PM
Friday, January 25, 2013 9:54 AM -
If you want to use prototype so it can be overridden you can use revealing prototype pattern.
function myFunc(input) { // state this.foo = input; } myFunc.prototype = function () { // private var privateF = function () { return "private"; }; //public var publicF = function () { return "public: " + this.foo; }; return { publicFunc: publicF }; }(); var func = new myFunc("hello"); if(!func.privateF){ alert(func.publicFunc()); }
- Marked as answer by Song Tian Thursday, January 31, 2013 12:49 PM
Saturday, January 26, 2013 9:17 PM
All replies
-
Hi,
You can use the constructor function. And refer to the follow example:
function User(name) { this.name = name; } User.prototype.getName = function() { return this.name; }; var user = new User('Bob');
Roy
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help.- Marked as answer by Song Tian Thursday, January 31, 2013 12:49 PM
Friday, January 25, 2013 9:54 AM -
Hi,
Thanks for the reply. But, I don't want to use the `this` variable, as its makes variable public.
My requirement is to keep the variables private, which can be accessed only through getter/setters.
Regards,
Abhinav Kumar
Friday, January 25, 2013 10:30 AM -
As far as I know if you are using the prototype pattern then you will have to use "this".
It sounds to me that you actually want a closure/module pattern as this will give you protected members similar to how you may be used to in C#.
function myFunc() { var privateV = "private"; var publicV = "public"; return { publicName : publicV } } var funcInst = new myFunc();
if (!funcInst.privateV) {alert(funcInst.publicName);}
You specify what functions/variables you want to make public in an object literal inside return {}.
A difference with this pattern is that the function is loaded into memory each time you create a new object whereas with prototype its loaded only once regardless of how many objects you create. Generally this should not be an issue unless you are doing some logic that creates lots of objects in a loop.
Is that what you were after?
- Edited by Tech Guy 8008 Saturday, January 26, 2013 6:01 PM code error
Saturday, January 26, 2013 5:22 PM -
If you want to use prototype so it can be overridden you can use revealing prototype pattern.
function myFunc(input) { // state this.foo = input; } myFunc.prototype = function () { // private var privateF = function () { return "private"; }; //public var publicF = function () { return "public: " + this.foo; }; return { publicFunc: publicF }; }(); var func = new myFunc("hello"); if(!func.privateF){ alert(func.publicFunc()); }
- Marked as answer by Song Tian Thursday, January 31, 2013 12:49 PM
Saturday, January 26, 2013 9:17 PM