Static variables and functions, instance variables and functions, and prototype instructions

Source: Internet
Author: User

static variables, functions

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 instance is not accessible, and such variables and functions are called static variables and static functions, which are well understood by the students of Java and C #.

1 functionObj () {2             }3 4obj.a=0;//Static Variables5 6obj.fn=function(){//static Functions7 8             }9Console.log (OBJ.A);//0TenConsole.log (typeofOBJ.FN);//function One  Avaro=NewObj (); -Console.log (O.A);//undefined -Console.log (typeofO.FN);//undefined

instance variables, functions

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.

1 functionObj () {2 This. a=[];//instance Variable3 This. fn=function(){//instance Method4 5                 }6             }7 8Console.log (typeofOBJ.A);//undefined9Console.log (typeofOBJ.FN);//undefinedTen  Onevaro=NewObj (); AConsole.log (typeofO.A);//Object -Console.log (typeofO.FN);//function

This can be achieved for the above purposes, however

1 functionObj () {2 This. a=[];//instance Variable3 This. fn=function(){//instance Method4 5                 }6             }7 8 varo1=NewObj ();9O1.a.push (1);Teno1.fn={}; OneConsole.log (o1.a);//[1] AConsole.log (typeofO1.FN);//Object -varO2=NewObj (); -Console.log (o2.a);//[] theConsole.log (typeofO2.FN);//function

The above code runs exactly as expected, but it also illustrates a problem where a and FN are modified in O1, but not changed in O2, since arrays and functions are objects and reference types, which means that the properties and methods in O1 and the properties and methods in the O2 have the same name but are not a reference. Instead, a copy of the properties and methods defined by the Obj object.

This is not a problem for the property, but for the method of the problem is very large, because the method is to do exactly the same function, but two copies, if a function object has thousands and instance methods, then each instance of it to maintain a copy of thousands of methods, which is obviously unscientific, this can be swollen to do it, Prototype was born.

Prototype

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, some around, write code,!

1 function Person () {23 }

As you can see, the person object automatically gets the Prototyp property, and prototype is an object that automatically gets a constructor property that points to the person object.

When the constructor is called to create an instance, the instance will contain an internal pointer (many browsers with the pointer name __proto__) to the prototype of the constructor, which exists between the instance and the prototype of the constructor, not between the instance and the constructor.

1 functionPerson (name) {2 This. name=name;3             }4 5Person.prototype.printname=function(){6Alert This. Name);7             }8 9varperson1=NewPerson (' Byron ');TenvarPerson2=NewPerson (' Frank ');

The instance of person Person1 contains the Name property and automatically generates a __proto__ property that points to the prototype of the person who can access the Printname method defined in prototype, presumably this way.

Write a program test to see the prototype inside the property, method is able to share

1 functionPerson (name) {2 This. name=name;3             }4 5Person.prototype.share=[];6 7Person.prototype.printname=function(){8Alert This. Name);9             }Ten  Onevarperson1=NewPerson (' Byron '); AvarPerson2=NewPerson (' Frank '); -  -Person1.share.push (1); thePerson2.share.push (2); -Console.log (Person2.share);//[up]

Sure enough In fact, when the code reads a property of an object, it performs a search, the target is a property with the given name, the search begins with the object instance, if the attribute is found in the instance, and if not, finds the prototype, If it is still not found, continue to recursively prototype the prototype object until it is found, and if recursive to object still does not return an error. Similarly, if you define a property or function in an instance that has the same name as prototype, the prototype property or function is overwritten.

1 functionPerson (name) {2 This. name=name;3             }4 5Person.prototype.share=[];6varperson=NewPerson (' Byron ');7Person.share=0;8 9Console.log (Person.share);//0 instead of prototype []

Constructing Simple objects

Of course prototype is not specifically defined to solve the problem above, but it solves the problem. By understanding this knowledge, you can build a scientific, reusable object, and if you want the properties or functions of an instance object to be defined in prototype, you can pass the instantiation parameter through the constructor if you want the property or method that each instance has to be defined in this.

1 functionPerson (name) {2 This. name=name;3             }4 5Person.prototype.share=[];6 7Person.prototype.printname=function(){8Alert This. Name);9}

Static variables and functions, instance variables and functions, and prototype descriptions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.