Reputation: 35194
var Foo = (function () {
var foo = function() { };
var privateMethod = function(){ };
foo.prototype = {
init: function() {
console.log(this.privateMethod); //undefined
}
};
return foo;
})();
I know that I can access privateMethod
directly without using the this
pointer. But since I come from the c# world, I would like to use it for readability purposes.
Is there any way to reference my "private methods" using a pointer?
Upvotes: 0
Views: 60
Reputation: 71908
You can't. You can only use this
to refer to "public" methods. If you really want to use a something.method
notation, you could use:
var Foo = (function () {
var foo = function() { };
var private = {
privateMethod : function(){ };
}
foo.prototype = {
init: function() {
console.log(private.privateMethod);
}
};
return foo;
})();
Upvotes: 2
Reputation: 234795
privateMethod
is not specific to each instance of foo
. Just reference it without the this.
qualifier—although you probably want to log the results of a function call, not the function itself:
console.log(privateMethod());
Upvotes: 2