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 pop-up value is undefined //alert(null==name);//true if(null == name){ = name; } } function b(name){ //alert(name);//The pop-up value is undefined //false, no reassignment, or the value created by new="TOm" if(null != name){ = name; } } //Grape construction methodfunction c(name){ //alert(name);//The pop-up value is undefined //If name is true, no matter what is followed, the first value will be returned directly //If name is false, no matter what is followed, return the following value = name || "Jack";//If name is empty, assign the value to the following Jack} //Construction method without parametersfunction d(){ } = "Tom"; = "Tom"; = "Tom"; = "Tom"; //Grape construction methodalert(new a().name); //undefined alert(new b().name);//Tom alert(new c().name);//Jack alert(new d().name);//Use 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, readers who are interested in reading this site's special topic:Summary of JavaScript switching effects and techniques》、《Summary of JavaScript search algorithm skills》、《Summary of JavaScript animation special effects and techniques》、《Summary of JavaScript Errors and Debugging Skills》、《Summary of JavaScript data structure and algorithm techniques》、《JavaScript traversal algorithm and skills summary"and"Summary of JavaScript mathematical operations usage》
I hope this article will be helpful to everyone's JavaScript programming.