JavaScript constructor function: public,private,privileged,static method
Blogs20132013-09-17
JavaScript constructor function: public,private,privileged,static method
The following includes various JavaScript methods for a constructor function:
- public method - prototype
- privileged method - function method with this
- private method - closure method without this
- Public static method
function Person(name) {
this.name = name || "public";
// 1. privileged
this.getName = function() {
return this.name + ', ' + privateMethod();
}
// 2. private
var privates = 'private';
function privateMethod() {
return privates;
}
}
// Public methods
Person.prototype = {
getDate: function() {
return new Date();
},
getWorld: function(){
return this.name;
}
}
// Public Static method:
Person.act = function() {
return Person.constructor.prototype.constructor;
}
var p = new Person();
p.getName('william Jiang');
p.getDate();
p.getWorld();
Person.act();