1.Global object. This object is special because it does not exist!-_-. If you declare
var Pointer=global;
An error was detected and the object could not be found. This is because in ECMAScript, each function is a method of an object, the isNaN (), Isfinite (), parseint (), and parsefloat () functions that we use, appear to be independent functions, but they are all functions of the global object.
Note the two functions used to process URL encoding:
1) encodeURI () and decodeURI () process the complete URI
2) encodeURIComponent () and decodeuricomponent () processing fragments
2. Other objects, such as the Array,math,date object, I find more interesting is that the array is handled in much the same way as the array in Ruby.
How to create a 3.ECMAScript object:
1) Factory mode:
function showColor(){
alert(this.color)
}
function createCar(sColor, iDoors, iMpg) {
var oTempCar = new Object;
oTempCar.color = sColor;
oTempCar.doors = iDoors;
oTempCar.mpg = iMpg;
oTempCar.showColor =showColor;
return oTempCar;
}
var oCar1 = createCar("red", 4, 23);
var oCar2 = createCar("blue", 3, 25);
It looks strange in this way, as if the method Showcolor () is not a method of the object
2 constructor Mode:
function Car(sColor, iDoors, iMpg) {
this.color = sColor;
this.doors = iDoors;
this.mpg = iMpg;
this.showColor = function () {
alert(this.color)
};
}
var oCar1 = new Car("red", 4, 23);
var oCar2 = new Car("blue", 3, 25);
The new problem with this approach is that every time you construct an object, it repeats the build function Showcolor, creating a separate version of the function for each object.
3) Prototype mode
function Car() {
}
Car.prototype.color = "red";
Car.prototype.doors = 4;
Car.prototype.mpg = 23;
Car.prototype.drivers = new Array("Mike", "Sue");
Car.prototype.showColor = function () {
alert(this.color);
};
var oCar1 = new Car();
var oCar2 = new Car();
oCar1.drivers.push("Matt");
alert(oCar1.drivers); //outputs "Mike,Sue,Matt"
alert(oCar2.drivers); //outputs "Mike,Sue,Matt"
The object is constructed using the prototype property of the object, but there are two problems: there is no way to use constructor arguments to generate objects; functions are shared by different objects, but properties are also shared, such as in the code above, The drivers property of the OCAR1 is the same array object as the OCar2 drivers property.