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

      šŸ“§ jxjwilliam@gmail.com

    • Version: ā€šŸš€ 1.1.0
  • JavaScript static method and instance method

    Blogs20142014-05-03


    JavaScript static method

    For the JavaScript MVC, the nouns of Class, staticProperty, prototypeMethod, Static method, instance method, it’s to confusion. Here is my codes to test Static method’s relationship with Class, and it can’t use ā€˜this’ to refer to properties/methods of the Class.

    var Person = function(name) {
      this.name = name;
      this.say = function() {
        console.log('hello ' + this.name);
      }
    }
    
    Person.StaticMethod = function() {
      var sm = new this('relationship');
      sm.say();
      console.log(sm instanceof Person);
      console.log(sm instanceof Person.StaticMethod);
    }
    
    var p = new Person('stranger');
    p.say();  // hello stranger
    
    Person.StaticMethod();
    //return:
    // hello relationship
    // true false 

    The following are some general Static methods:

    • findAll: ā€˜GET /todos.json’,
    • findOne: ā€˜GET /todos/{id}.json’,
    • create: ā€˜POST /todos.json’,
    • update: ā€˜PUT /todos/{id}.json’,
    • destroy: ā€˜DELETE /todos/{id}.json’

    And, for instance method (such as jQuery.fn), normally are used as:

    • save, e.g. new obj().save()
    • update, e.g. instance.update()
    • bind, e.g. $(el).bind()
    • serialize, e.g model.serialize()