Simple example illustrating private and public properties for JavaScript class
Posted: September 29, 2012 | Author: asv3 | Filed under: Uncategorized | Tags: class, javascript, private, public |
var log = console.log;
var Class = function (){</pre>
var privateVar = 'this is secrete';
var privateMethod = function (){
return privateVar;
}
return {
publicMethod: function (){
return privateMethod()
},
publicVar :'this is public'
}
}
var instance = new Class()
log(instance.publicMethod()) // this is secret
log(instance.publicVar) // this is public
log(instance.privateMethod()) //Error
Recent Comments