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); // The undefined p. cry (); // Uncaught TypeError: Object # <Person> has no method 'cry' is displayed'
It can be found that the instance object cannot have static methods and attributes of the category. Only class names are allowed for access.
<span style="color:#333333;">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</span>
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"]);//1You are welcome to further discuss and make progress together!
(Javascript) Static scopes are not static. How can this problem be solved?
The alternate () method is a sub-Method of sayHi (). In fact, this is not true. It should be an attribute.
Javascript) Static scopes are not static. How can we understand them?
Think about it:
Although many books say that javascript is not like this, in the final analysis, javascript still has the existence of objects, such as window objects. It encapsulates the scopes of a certain field,
For example, window. open (). If you use the open () method without adding a window, you can understand that the static scope is not static. That is to say, You didn't add a reference to the object, for example, for an array, each time we define an array, if we want to access an element of its array, we have to add the subscript of the array to access it.
I will help you change the above program. Maybe you will understand the truth.
Function sayHi ()
{
Alert ("hi ");
Var alternate = alternte
// If alternte is added with (), the local variable accepts the value returned by the following function.
// If alternte is not added with (), it is equal to the sum used to execute the function defined below.
Function alternate ()
{
Return alert ("hola ");
// Alert ("hola ");
};
}
The above Code encapsulates a function. If you want to call it, you have to create an object.
Var sa = new sayHi ();
Then you can use sa as an object. You can use sa as a reference variable to call some attributes and methods defined in you. As follows:
Sa. alternate
You can call alternate () by referencing this sa ()
You can also use the sa object to directly call alternate ()
Example: sa. alternate ()
It's a mess. I wonder if you can understand it.
The static method in JavaScript can use the object method ()?
This method is a static method of the Date object. Generally, this method is called in the form of Date. parse () instead of using dateobject. parse.