I think you are a little confused. If you use Demo
as a class, you do not want to call it as a function, but rather as an instance.
When used with instantiation, you can do this:
function Demo(){ var abc = "some value"; this.setAbc = function(val){ return abc = val; } this.getAbc = function(){ return abc; } } var d = new Demo(); d.getAbc()
These types of functions (defined in the "constructor") are called privileged functions. They have access to both public (i.e. defined on the prototype) and private variables. Read this for a good cut for public / private / privileged class members.
note that if you just do:
var d = Demo();
You are not getting an instance of Demo, you are just getting what it returns. In my case, undefined
.
change
After reading your message again, the quick reply will be NO, and not with your specific definition, you will need to do something like what I am doing.
OR if you stick to your paradigm:
function Demo(){ var abc = "some value"; return { get test(){ return abc; }, set test(val){ abc = val; } } }
source share