JavaScript prototype prototype

Source: Internet
Author: User
Tags instance method

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

Related Article

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.