Use JAVASCRIPT to implement static objects, static methods, and static attributes

Source: Internet
Author: User

Use JAVASCRIPT to implement static objects, static methods, and static attributes

The object-oriented feature of Javascript language is very weak. When other object-oriented languages create a class, they only need to use the keyword static to specify the class as a static class. Javascript does not provide such keywords as static, to make Javascript "static" only relies on some "tricks and tricks.

The code lists two static methods/attribute implementation methods,One is static methods and attributes of static classes., The other isStatic methods and attributes of non-static classesThe code description is written in the code comments of each line, so it will not be repeated here.

/****************************************
* Method 1
* All classes, methods, and properties are static types.
* You cannot create an instance.
*****************************************/
var Time = {
    today: ‘2009-3-8′, 
    weather: ‘rain’, 
    show: function() {
alert(‘Today is ‘ + this.today); 
}
}; 
 
// Static objects can be used directly without the need to create an instance
alert(‘It is ‘ + Time.weather + ‘ today.’); 
Time.show(); 
 
// The following code will fail because the static class cannot create an instance.
//var t = new Time();
//t.show();
 
/****************************************
* Method 2
* Common objects with static and non-static attributes and Methods
* Instantiation can be used.
* Note:
* 1. Use the class name to access static methods/Properties
* 2. Use the Instance name to access non-static methods/attributes
*****************************************/
function Person(name) {
// Non-static attributes
this.name = name; 
// Non-static method
this.show = function() {
alert(‘My name is ‘ + this.name + ‘.’); 
}
}
// Add static attributes. All users are talking.
Person.mouth = 1; 
// Add a static method.
Person.cry = function() {
alert(‘Wa wa wa …’); 
}; 
// Use the prototype keyword to add non-static attributes. Each user may have different teeth.
Person.prototype.teeth = 32; 
 
// Non-static methods must be accessed through class instances
var me = new Person(‘Zhangsan’); 
// Use non-static methods and attributes
me.show(); 
alert(‘I have ‘ + me.teeth + ‘ teeth.’); 
// Use static methods and attributes
Person.cry(); 
alert(‘I have ‘ + Person.mouth + ‘ mouth.’);
//
Var p = new Person ("x"); alert (p. mouth); // undefined p. cry (); // Uncaught TypeError: Object #
 
  
Has no method 'cry'
 
It can be found that the instance object cannot have static methods and attributes of the category. Only class names are allowed for access.
 
function Person(name){this.name=name;this.show=function(){alert("My Name is "+this.name);};};Person.mouth=1;Person.cry=function(){alert("wa wa");}Person.prototype.teeth=32;var p=new Person("x");alert(Person["mouth"]);//1p["show"]();//My name is x


 
In Jquery, you can use the static methods and attributes of the object as follows:
 
var arr=new Array();arr["push"]("x");alert(arr["length"]);//1
You are welcome to further discuss and make progress together!
 
 
 
 

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.