JavaScript 演练(10). 谁的 this ?


MyClass = function () {
    this.A = 1;
}

MyClass.prototype.X = function () {
    this.B = 2;
}

MyClass.prototype.Y = function () {
    this.Z = function () {
        this.C = 3;
    }
}

/* 内部对象的 this ? */
obj = new MyClass();
alert(obj.A);        //1

obj1 = new obj.X();
alert(obj1.B);       //2

obj2 = new (new obj.Y()).Z();
alert(obj2.C);       //3

/* 所属对象的 this ? */
obj = new MyClass();
obj.X();
obj.Y();
obj.Z();
alert(obj.A); //1
alert(obj.B); //2
alert(obj.C); //3

原文地址:https://www.cnblogs.com/del/p/2406239.html