This article describes the creation method of JS prototype object. Share it for your reference, as follows:
When using js' prototype property, the method of creating objects is priority
1. If there is a construction method in the method, use the construction method in the method first.
2. If there is no constructor in the method, continue to look for the constructor of the prototype prototype.
<html><head><TITLE>class_obj_js_class</TITLE><script language=javaScript>function a(name){ //alert(name);//The value popped up is undefined //alert(null==name);//true if(null == name){ this.name = name; }}function b(name){ //alert(name);//Is the value popped up undefined //false, this.name has not been reassigned, or is the value created by new prototype.name="TOm" if(null != name){ this.name = name; }}//The parameter constructor function c(name){ //alert(name);//The pop-up value is undefined //If name is true, the first value will be returned directly regardless of what is followed//If name is false, the next value will be returned directly regardless of what is followed this.name = name || "Jack";//If name is empty, the value will be assigned to the next Jack}//No parameter constructor function d(){}a.prototype.name = "Tom";b.prototype.name = "Tom";c.prototype.name = "Tom";d.prototype.name = "Tom";//The parameter constructor alert(new a().name); //undefinedalert(new b().name);//Tomalert(new c().name);//Jackalert(new d().name);//Use the parameterless construction method</script><body >//body></html>Remark:
1. Generally, we add the attribute of "object" to the method
2. Add method after the prototype property
The purpose of this is to improve code reuse, and you can add methods to the object "infinitely" to facilitate expansion
Note: In order to improve the efficiency of JS, you should pay attention to limiting it to level one and two when using the prototype chain, because the browser will automatically loop through it. If the depth is too deep, it will affect the efficiency.
For more information about JavaScript related content, please check out the topics of this site: "Summary of JavaScript switching effects and techniques", "Summary of JavaScript search algorithm skills", "Summary of JavaScript animation effects and techniques", "Summary of JavaScript errors and debugging techniques", "Summary of JavaScript data structures and algorithm skills", "Summary of JavaScript traversal algorithms and techniques", and "Summary of JavaScript mathematical operations usage"
I hope this article will be helpful to everyone's JavaScript programming.