Each function has a prototype property, which is a reference to an object called the prototype object, which contains the methods and properties shared by the function instance, that is, when the function is used as a constructor call (called with the new operator). The newly created object inherits properties and methods from the prototype object.
When a function is defined, pass the "." The properties and functions that are added to it can still be accessed through the object itself, but its instances are not accessible, and variables and functions, respectively, are called static variables and static functions . For example, the following code:
function Obj () { }obj.a=0;//static variable obj.fn=function () {//static function } console.log (OBJ.A);//0 Console.log (typeof OBJ.FN); function var o=new Obj (); Console.log (O.A);//undefinedconsole.log (typeof O.fn);//undefined
In object-oriented programming, in addition to some library functions, we still want to define some properties and methods at the time of object definition, which can be accessed after instantiation, and JavaScript can do so, the code is as follows:
function Obj () { this.a=[];//instance variable this.fn=function () {//instance method }} Console.log (typeof obj.a); Undefined Console.log (typeof Obj.fn); Undefined var o=new Obj (); Console.log (typeof o.a); Object Console.log (typeof O.fn); function
Whenever a new function is created, a prototype property is created for the function based on a specific set of rules, and by default the prototype property is given a constructor (constructor) property by default. This property is a pointer to the function where the prototype property is located
JavaScript prototype prototype