William Jiang

JavaScript,PHP,Node,Perl,LAMP Web Developer – http://williamjxj.com; https://github.com/williamjxj?tab=repositories

JavaScript constructor function: public,private,privileged,static method

JavaScript constructor function: public,private,privileged,static method

The following includes various JavaScript methods for a constructor function:

  1. public method – prototype
  2. privileged method – function method with this
  3. private method – closure method without this
  4. 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();
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: