• Blogs (9)
    • πŸ“± 236 - 992 - 3846

      πŸ“§ jxjwilliam@gmail.com

    • Version: β€πŸš€ 1.1.0
  • 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:

    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();