Prototype in js represents the prototype of the function, and prototype represents a collection of attributes of a class; when an object of a class is generated through new, the attributes of the prototype object will become the attributes of the instantiated object; Inaccessible, that is, it becomes a private variable and private function.
The operating environment of this article: Windows 7 system, Dell G3 computer, javascript1.8.5.
The prototype object is an important mechanism for realizing object-oriented. Each function is also an object, and their corresponding class is function. Each function object has a sub-object prototype.
Prototype represents the prototype of the function, and prototype represents a collection of attributes of a class. When an object of a class is generated through new, the properties of the prototype object become the properties of the instantiated object.
This attribute is very useful: declare a common variable or function for a specific class.
If the variables and functions defined within the function do not provide an interface to the outside world, they will not be accessible from the outside, that is, they will become private variables and private functions.
function Obj(){ var a=0; //私有变量 var fn=function(){ //私有函数 } }
Static variables and functions
After defining a function, the properties and functions added to it through "." can still be accessed through the object itself, but its instances cannot be accessed. Such variables and functions are called static variables and static functions respectively. Students who have used Java and C# can easily understand the meaning of static.
function Obj(){ } Obj.a=0; //静态变量 Obj.fn=function(){ //静态函数 } console.log(Obj.a); //0 console.log(typeof Obj.fn); //function var o=new Obj(); console.log(o.a); //undefined console.log(typeof o.fn); //undefined
Related learning recommendations: js video tutorial
The above is the detailed content of What is prototype in js. For more information, please follow other related articles on the PHP Chinese website!