Javascript:自我和这

时间:2013-02-25 09:27:12

标签: javascript prototype this self

任何人都可以解释为什么我会得到不同的自我和这个值?自我是对此的参考。

function Parent(){
   var self = this;
   this.func = function(){
      // self.a is undefined
      // this.a is 'Test'
      console.log(self.a, this.a);
   }
}

function Child(x){
   this.a = x;
}

Child.prototype.__proto__ = new Parent;
var ch = new Child('Test');
ch.func();

我一直在项目中使用self,这是我第一次遇到这个问题。

4 个答案:

答案 0 :(得分:9)

这是因为self引用了Parent的实例,但只有Child的实例具有a属性。

function Parent(){
   var self = this; // this is an instance of Parent
   this.func = function(){
      console.log(self.a, this.a);
   }
}

function Child(x){
    this.a = x; // Instances of Child have an `a` property
}

Child.prototype.__proto__ = new Parent;
var ch = new Child('Test');

ch.func(); // Method called in context of instance of Child

因此,当您在func的实例上调用Child时,this会引用该实例。这就是this.afunc内为您提供正确值的原因。

答案 1 :(得分:1)

func内,this指的是Child的实例。

Child.prototype.__proto__ = new Parent;

Child的原型设置为Parent的实例。因此,当ch.func()被调用时,func()位于Child的原型链上,但是在ch的上下文中调用Child <{1}} < / p>

self仍然指的是Parent的实例,其中没有属性a

进一步说明:

var p = new Parent();

// this.a is undefined because this is an instance of Parent
p.func(); // undefined undefined

答案 2 :(得分:0)

var myObject = {
    foo: "bar",
    func: function() {
        var self = this;
        console.log("outer func1:  this.foo = " + this.foo); //bar
        console.log("outer func2:  self.foo = " + self.foo); //bar
        (function() {
            console.log("inner func3:  this.foo = " + this.foo); //undefind
            console.log("inner func4:  self.foo = " + self.foo);//bar
        }());
    }
};
myObject.func();

答案 3 :(得分:0)

if (rawNumber < 1000000.0) {
        return rawNumber.ToString (formatString);
    }
    ScientificNotation scientificNotation = ScientificNotation.FromDouble (rawNumber);
    ushort adjustedExponent = (ushort)((scientificNotation.exponent / 3) - 1);
    string prefix = "";
    if (adjustedExponent < 10) {
        prefix = latin[adjustedExponent - 1];
    }
    else {
        ushort hundredsPlace = (ushort)(adjustedExponent / 100);
        ushort tensPlace = (ushort)((adjustedExponent / 10) % 10);
        ushort onesPlace = (ushort)(adjustedExponent % 10);
        string onesString = (onesPlace > 0) ? ones[onesPlace - 1] : "";
        string modifier = "";
        if ((onesPlace == 7) || (onesPlace == 9)) {
            if (tensPlace > 0) {
                if ((tensPlace == 2) || (tensPlace == 8)) {
                    modifier = "m";
                }
                else if (tensPlace != 9) {
                    modifier = "n";
                }
            }
            else if (hundredsPlace > 0) {
                if (hundredsPlace == 8) {
                    modifier = "m";
                }
                else if (hundredsPlace != 9) {
                    modifier = "n";
                }
            }
        }
        if ((onesPlace == 3) || (onesPlace == 6)) {
            if (tensPlace > 0) {
                if ((tensPlace == 2) || (tensPlace == 3) || (tensPlace == 4) || (tensPlace == 5) || (tensPlace == 8)) {
                    modifier = ((onesPlace == 6) && (tensPlace == 8)) ? "x" : "s";
                }
            }
            else if (hundredsPlace > 0) {
                if ((hundredsPlace == 1) || (hundredsPlace == 3) || (hundredsPlace == 4) || (hundredsPlace == 5) || (hundredsPlace == 8)) {
                    modifier = ((onesPlace == 6) && ((tensPlace == 1) || (tensPlace == 8))) ? "x" : "s";
                }
            }
        }
        string tensString = (tensPlace > 0) ? tens[tensPlace - 1] : "";
        string hundredsString = (hundredsPlace > 0) ? hundreds[hundredsPlace - 1] : "";
        prefix = string.Format ("{0}{1}{2}{3}", onesString, modifier, tensString, hundredsString);
    }

    double adjustedSignificand = scientificNotation.significand * Math.Pow (10, scientificNotation.exponent % 3);
    double integralPart = Math.Truncate (adjustedSignificand);
    //return string.Format("{0} {1}llion", (((adjustedSignificand - integralPart) > maxFractional) ? integralPart + maxFractional : adjustedSignificand).ToString (formatString), prefix.TrimEnd ('a'));
    return string.Format("{0} {1}", (((adjustedSignificand - integralPart) > maxFractional) ? integralPart + maxFractional : adjustedSignificand).ToString(formatString), prefix);
    //return "" + " " + "";

}
function Foo() {
  this.bar = 'bar';
  return this;
}
Foo.prototype.test = function(){return 1;}



function Bar() {
  this.bro = 'bro';
  return this;
}
Bar.prototype.test2 = function(){return 2;}

function Cool2() {
  Foo.call(this);
  Bar.call(this);

  return this;
}
//console.log(Object.create(Bar.prototype));
var combine = Object.create(Foo.prototype);
//console.log(combine);

$.extend(combine, Object.create(Bar.prototype));
$.extend(combine, Object.create(Foo.prototype));

Cool2.prototype = Object.create(combine);
//Cool2.prototype.constructor = Cool2;

var cool = new Cool2();
 

console.log(cool.test()); // 1
console.log(cool.test2()); //2
console.log(cool.brow) //bro
console.log(cool.bar) //bar
console.log(cool instanceof Foo); //true
console.log(cool instanceof Bar); //false