简体   繁体   English

javascript对象原型此参考

[英]javascript object prototype this reference

var x = (arg1, arg2) {
  this.y = arg1;
  this.z = arg2;
}

x.prototype.a = function() {
  var self = this;
  some_obj1.on('data', function() {
    self.y = 'new y value';
  });
}

x.prototype.b = function() {
  var self = this;
  some_obj2.on('data', function() {
    self.z = 'new z value';
  });
}

Is there any way to declare self as an instance variable (without using 'this' obviously) so that it doesn't need declaring within every function? 有没有什么方法可以将self声明为实例变量(显然不使用'this'),从而无需在每个函数中都声明它? So for example the declaration of 'a' would be: 因此,例如,“ a”的声明为:

x.prototype.a = function() {
  ob2.on('data', function() {
    self.z = 'some new value';
  });
}

Hopefully this example is clear enough, it's not tested (written on the fly when asking the question) and more pseudo code but should get the point across.. 希望这个例子很清楚,没有经过测试(问问题时即时编写)和更多的伪代码,但应该可以理解。

The best thing to do is to partially apply arguments. 最好的办法是部分应用参数。 The below is a cross browser implementation of the newer Function.prototype.bind . 以下是更新的Function.prototype.bind的跨浏览器实现。 project.bind , using the below implementation, will use native Function.prototype.bind if it is available or the custom implementation if the native one is not available. 使用以下实现的project.bind将使用本机Function.prototype.bind如果可用)或自定义实现(如果本机不可用)。

Update I created a working Fiddle . 更新我创建了一个工作的小提琴

project = {};
project.bindJs_ = function(fn, selfObj, var_args) {
  if (!fn) {
    throw new Error();
  }

  if (arguments.length > 2) {
    var boundArgs = Array.prototype.slice.call(arguments, 2);
    return function() {
      // Prepend the bound arguments to the current arguments.
      var newArgs = Array.prototype.slice.call(arguments);
      Array.prototype.unshift.apply(newArgs, boundArgs);
      return fn.apply(selfObj, newArgs);
    };

  } else {
    return function() {
      return fn.apply(selfObj, arguments);
    };
  }
};
// A router for the native Function.prototype.bind
project.bindNative_ = function(fn, selfObj, var_args) {
  return /** @type {!Function} */ (fn.call.apply(fn.bind, arguments));
};



   project.bind = function() {
       if (Function.prototype.bind &&
           Function.prototype.bind.toString().indexOf('native code') != -1) {
           project.bind = project.bindNative_;
       } else {
           project.bind = project.bindJs_;
       }
       return project.bind.apply(null, arguments);
    };

Now you can do this: 现在您可以执行以下操作:

x.prototype.a = function() {
  ob2.on('data', project.bind(function() {
    // the this. object inside the function will now point to x.
    this.z = 'some new value';
  }, this, any, argument, you, want, to, pass));
}

No, you can't. 不,你不能。 You'd need to modify the scope chain in some way to avoid using this at all. 您需要以某种方式修改范围链,以完全避免使用this A slightly cleaner way would be to use Function#bind to specify this . 一种更简洁的方法是使用Function#bind来指定this

x.prototype.a = function() {
  ob2.on('data', function() {
    this.z = 'some new value';
  }.bind(this));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM