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