JS中创建对象的方式有工厂方式、构造函数方式等等。
工厂方式的过程很容易理解
function createCar() {
    var oTempCar = new Object;
    oTempCar.color = "red";
    return oTempCar;
}var oCar = createCar();构造函数方式的过程是这样:
function Car(sColor) {
    this.color = sColor;
}var oCar = new Car("red");
书上这样写的:使用new运算符调用构造函数时,在执行前先创建一个对象,只有用this才能访问对象但是用对象冒充实现继承时
function Car(sColor) {
    this.color = sColor;
}function Lorry(sColor, sName) {
    this.newMethod = Car;
    this.newMethod(sColor);
    delete this.newMethod;
    
    this.name = sName;
}var oLorry = new Lorry("blue", "Foton");那么在Bus的构造函数中,首先将this.newMethod指向Car的构造函数,然后再调用this.newMethod(sColor)。在这里并没有使用new运算符,那么该怎样理解呢?书上说,this.newMethod()中的this指向的是所属的对象,也就是new Lorry("blue", "Foton")所产生的对象,我觉得这样很难理解

解决方案 »

  1.   

    写错了,最后一段第一句的Bus应为Lorry
    不能修改...
      

  2.   

    这种方法似乎很巧妙,学习了this.newMethod = Car; 
    this.newMethod(sColor); 
    delete this.newMethod;相当于是执行了Car.call(this, sColor); 
      

  3.   

    你把
    this.newMethod = Car
    理解为this.newMethod = function(sColor) {
      this.color = sColor;
    };就不难理解了呀
      

  4.   

    js里面函数也是对象,创建函数时,比如function fun1(){},此时fun1是函数名,其实它是储存的是新建函数对象的地址,this.newMethod = Car实际上相当于吧car储存的地址赋给newMethod,此时car和newMethod同时指向一个函数对象