Static variable and instance property of JavaScript

Source: Internet
Author: User
Static variable (static variable ?) :
The feature is that you can directly access it through the function name, or access it through the object instance and share it with all instances. modifications to it will affect all instances, similar to static variables sold in Java.
Instance property (instance variable ?) :
It is only valid for the current instance.

Example of static variable:

 function dosum (x, y) {dosum. sum = dosum. sum + x + y; return dosum. SUM;} dosum. sum = 0; // defines static variablealert (dosum (1, 1); // result = 2 alert (dosum (1, 1 )); // result = 4var O1 = dosum (1, 1); var O2 = dosum (1, 1); alert (O1); // result = 6 alert (O2 ); // result = 8 
 

example of instance property:

 var O1 = new object (); o1.yes = true; var O2 = new object (); o2.no = false; Al ERT (o1.yes); // result = truealert (o1.no); // result = undefinedalert (o2.yes); // result = undefinedalert (o2.no ); // result = false 
 

check whether the static method is different:

 function dosum () {

}< br>
dosum. show = function () {
return "111111";
}< br>
var o = new dosum ();
alert (O. show (); // error returned: O. show is not a fun Ction
alert (dosum. show (); // result = 11111
 

it seems that static methods cannot be called through instances.

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.