I'm trying to create encapsulated objects that can be inherited and that possess some public properties. Here's some code:
var MyObject = (function()
{
function myObject()
{
//Public variables
this.publicVar1 = 'hello';
this.publicVar2 = 'world';
}
function foo()
{
console.log(this.publicVar1 + ' ' + this.publicVar2);
}
function bar()
{
foo();
}
//Public API
myObject.prototype = {
constructor: myObject,
foo: foo,
bar: bar
}
return myObject;
})();
var mo = new MyObject();
mo.foo(); //hello world
mo.bar(); //undefined undefined
My question why exactly does mo.bar() log 'undefined undefined' and how can I fix this issue?
I'm trying to keep publicVar1 and publicVar2 in scope internally to the MyObject module but also make them publicly accessible and inherited by any other objects that extend the myObject prototype.
Thanks.