JavaScript prototype chain and privileged methods

Source: Internet
Author: User

<pre name= "Code" class= "JavaScript" >function ClassA () {        var value=4;        this.getvalue= function ()        {            return value;        }        this.setvalue= function (value)        {            this.value=value;        }     }            var instance= new ClassA ();            document.write (Instance.getvalue ());            Classa.setvalue (1);            document.write (Instance.getvalue ());            document.write (Instance.value);



The output is: 4,4,1.


The reason is that var value is a private variable, and it is not the same value as This.value.


Below we analyze how object instances are created and how private variables are accessed.


1. Instance properties and prototype properties


When a JavaScript object creates an instance, instance properties and methods can be assigned to an instance only through this in the constructor. If you want to create a shared property or method, you can share it through the prototype chain.

To create a shared property:

     function ClassA () {        var value=4;        this.getvalue= function ()        {            return value;        }        this.setvalue= function (value)        {            this.value=value;        }     }    classa.prototype.value=1;//shared Value    var  instance1=new ClassA (),//         instance2=new ClassA ();   Consloe.log (instance1.value);//1   Consloe.log (instance2.value);//1   Console.log (classa.value);// Undefined   


So the private property in ClassA () value can we access through Classa.value? The answer is no.

We analyze the lookup of identifiers in the object instance. When an instance looks for an attribute, it is divided into two steps:

(1). Find in instance properties of the instance itself.

(2). If the instance attribute is not found, the prototype of the object is located along the prototype chain.

Classa.value is not found in the instance properties of ClassA, then continues to find the prototype chain, ClassA is an instance of object, and therefore looks for Object.prototype.value, with a clear value of undefined.

2. Access to private variables

We know that JavaScript does not have block-level scopes, but we can use functions to simulate block-level scopes. The variables in the block-level scope are called private variables. Private variables, external inaccessible, we can inside the function

Generate privileged methods (privileged methods: methods that have access to private variables), where the privileged method is This.getvalue (); Created by constructors is a way to create privileged methods.


Another way to create a privileged method is to: a static method.

function ClassA () {        var value=4;        this.getvalue= function ()        {            return value;        }        this.setvalue= function (value)        {            this.value=value;        }        Classa.getvalue=function () {  //static method            return value;        }     }






JavaScript prototype chain and privileged methods

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.