Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从绑定到事件的另一个函数调用绑定到事件的函数是';行不通_Javascript_Jquery_Slickgrid - Fatal编程技术网

Javascript 从绑定到事件的另一个函数调用绑定到事件的函数是';行不通

Javascript 从绑定到事件的另一个函数调用绑定到事件的函数是';行不通,javascript,jquery,slickgrid,Javascript,Jquery,Slickgrid,我正在为slickgrid制作一个自定义编辑器,尽管我怀疑在这种情况下我所做的事情是否真正重要。假设我有这样的模糊设置: function TestEditor(args) { var $test0, $test1; //other variables this.init = function () { //various init stuff, it all works fine //init $test0 and $test1, also works fine $tes

我正在为slickgrid制作一个自定义编辑器,尽管我怀疑在这种情况下我所做的事情是否真正重要。假设我有这样的模糊设置:

function TestEditor(args) {
 var $test0, $test1;
 //other variables

 this.init = function () {
  //various init stuff, it all works fine
  //init $test0 and $test1, also works fine

  $test0.bind("change", this.test0Changed);
  $test1.bind("change", this.test1Changed);

  this.test0Changed(); //this works fine too, makes the nested call to test1Changed
 }

 this.test0Changed = function() {
  //does various operations
  this.test1Changed(); //calls test1Changed, this works _unless_ test0Changed is called through an event, then the code breaks here!
  //stuff that won't happen when the code breaks at the previous call
 }

 this.test1Changed = function() {
  //stuff, works fine unless called by test0Changed triggered by an event, then nothing
 }

 //whatever, lots of other stuff, it all works

 this.init();
}

我希望test0Changed调用test1Changed,如果我在代码中显式地调用this.test0Changed(),则效果很好。但是,当“change”事件触发test0Changed时,代码在尝试调用this.test1Changed()时中断。如果我注释掉对this.test1Changed()的调用,一切都很好,因此我知道正是这一行导致了问题。这是什么原因造成的?

这是因为当您将函数设置为
.bind()
时,它不会“记住”初始

作为处理程序,
将是接收事件的元素

this.init = function () {

   var self = this;

   $test0.bind("change", function() {self.test0Changed.apply(self, arguments);});
   $test1.bind("change", function() {self.test1Changed.apply(self, arguments);});

 }
在这里,我引用了要在变量中使用的
this
,并传递了使用该引用的
this
值调用函数的匿名函数


我还使用了
.apply
来确保传递所有原始参数。如果没有必要,你可以把它改成这个

this.init = function () {

   var self = this;

   $test0.bind("change", function() {self.test0Changed();});
   $test1.bind("change", function() {self.test1Changed();});

 }

或者您可以使用jQuery的
$.proxy
来保留
这个

this.init = function () {

   $test0.bind("change", $.proxy(this, 'test0Changed'));
   $test1.bind("change", $.proxy(this, 'test1Changed'));

 }

这是因为当您将函数设置为
.bind()
时,它不会“记住”初始

作为处理程序,
将是接收事件的元素

this.init = function () {

   var self = this;

   $test0.bind("change", function() {self.test0Changed.apply(self, arguments);});
   $test1.bind("change", function() {self.test1Changed.apply(self, arguments);});

 }
在这里,我引用了要在变量中使用的
this
,并传递了使用该引用的
this
值调用函数的匿名函数


我还使用了
.apply
来确保传递所有原始参数。如果没有必要,你可以把它改成这个

this.init = function () {

   var self = this;

   $test0.bind("change", function() {self.test0Changed();});
   $test1.bind("change", function() {self.test1Changed();});

 }

或者您可以使用jQuery的
$.proxy
来保留
这个

this.init = function () {

   $test0.bind("change", $.proxy(this, 'test0Changed'));
   $test1.bind("change", $.proxy(this, 'test1Changed'));

 }

虚线上标注的
test
变量是什么?哎呀,那是愚蠢的,我的意思是什么是虚线上标注的
test
变量?哎呀,那是愚蠢的,我的意思是哦,那么,在这种情况下,有没有办法得到这个呢?让它对这两种调用都起作用吗?很好,$.proxy工作得很好,尽管你缺少了一个右括号。哦,那么,在这种情况下,有没有办法做到这一点呢?要让它对这两种调用都有效吗?很好,$.proxy工作得很好,尽管缺少了一个右括号。