If you have been in touch with JS for a long time, then you should be able to see whether I summed up the correct, if you have just started to learn, then through my summary, you can distinguish them faster, remember I just contact JS, this piece is fuzzy for a long time!
1, the difference of looks:
1 function Class () {//Declaration of a class
2 This.constructmethod = function () {}; Adding a constructed construction method
3};
4 Class.classcmethod = function () {}; Adding class methods
5 class.prototype.protocmethod=function () {};//Add prototype method
2, the difference of usage:
1 Class.classmethod ();//class method call directly
2 var instance = new Class ();
3 Instance.constructmethod ()///To construct the method instance to invoke the
4 Instance.protomethod ()///prototype method instance to invoke
3, the difference in performance:
A Class A method can only have one copy in memory because it belongs to the class itself, and in practice we do not use the class method generally. The main thing to write is to let you know it.
Both the B construction method and the prototype method are instances, but the construction method produces a new copy in memory each time new Class (). Usually this method is used to differ between instances. The construction methods for each instance do not affect each other. But apparently, it takes up memory again. The prototype method is just the opposite, it does not follow the new Class ()
Produces a new copy, which is only available in memory. You can implement sharing between instances. It also saves memory.
In summary: You generally do not use the class method in the development, will have the common method to make the prototype method, will have the individuality method to make the construction method.
4, the construction method mentioned above can also be transferred to the instance in the actual project, that is, to add the method to the instance, because we usually put some attribute members instead of methods in the constructor of the class. See below:
1 function Class () {};
2 var instance = new Class ();
3 instance.instancefn=function () {};//Add instance method
Author: cnblogs Wimber
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/script/