ECMAScript Object Base

Source: Internet
Author: User
Tags constructor

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.

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.