简体   繁体   English

Javascript传递一个函数,存储其引用,将其值更改为另一个函数

[英]Javascript Pass a function, store its reference, change its value to another function

I have ObjectA, and objectB. 我有ObjectA和objectB。

I want to take control inside ObjectA when ObjectB has finished its tasks. 我想在ObjectB完成任务后在ObjectA内部进行控制。

So I have () : 所以我有 () :

ObjectB = function () 
  {
    this.loaded;  // it will be a fun 
    load_end = function () { this.loaded();} 
  } 

and also I have : 而且我有:

ObjectA.set_load_fun = function (ext_fun) { ext_fun = ObjectA.fake_fun;} 

and the code would be : 和代码将是:

ObjectA.set_load_fun (ObjectB.loaded);
ObjectB.load();
.....
// when ObjectB has finished calls load_end, and then calls this.loaded
// but this.loaded is really 'ObjectA.fake_fun'
// 

I don't want to write a event listener.... (or maybe I want to write 'builtin' events??) I want to do the same as ajax, filereader, etc. 我不想编写事件侦听器...。(或者也许我想编写“内置”事件??)我想做与ajax,filereader等相同的操作。

 xhr.onload     = function(evt) {  };
 xhr.onprogress = function (evt) {};

( In this case, I have not to define addeventlistener (xhr,"onload");} (在这种情况下,我不必定义addeventlistener(xhr,“ onload”);}

I simply would want to write 我只是想写

 ObjectB.loaded = ObjectA.fake_fun 
 ObjectB.loaded = global_object.function;
 ObjectB.loaded = mynamespaced.bla.bla.bla.function;

I hope I had explained well.... 我希望我已经解释清楚了...

Any help would be appreciated 任何帮助,将不胜感激

If I understood your question well, nearly everything looks good. 如果我很好地理解了您的问题,几乎一切都看起来不错。 You just need to assign loaded function to objectB, so you need to pass objectB into set_load_fun method or just assign this function. 您只需要将加载的函数分配给objectB,因此您需要将objectB传递给set_load_fun方法或仅分配此函数。

eg 1: 例如1:

ObjectA.set_load_fun = function (obj) { obj.loaded = ObjectA.fake_fun;} ;
ObjectA.set_load_fun (ObjectB);
ObjectB.load();

eg 2: 例如2:

ObjectB.loaded = ObjectA.fake_fun;
ObjectB.load();

Also if you need to use 'this' keyword to address ObjectA in your loaded function you need to use bind function. 同样,如果您需要在加载的函数中使用'this'关键字来寻址ObjectA,则需要使用bind函数。

I created JsFiddle example for you. 我为您创建了JsFiddle示例 Hope it will help. 希望它会有所帮助。

You need to wrap the function so it does contains the object in which context you want to evaluate the function. 您需要包装函数,以便它确实包含要在其中评估函数的上下文中的对象。

var A =  {
    prop: "i am in A",
    loaded: function ()  {
        console.log(this.prop); 
    }
}

var B =  {
    call_func: function (fn) {
       fn(); 
    }
}

B.call_func(A.loaded); // in console you will see undefined instead of "i am in A",
//because the function will be evaluated in context of window object

You need to wrap the loaded function so it is evaluated in context of A. 您需要包装已加载的函数,以便在A的上下文中对其进行评估。

B.call_func(function () {
     A.loaded(); 
}); // now you will see in console "i am in A"

So as you can see, in your code you can't use ObjectB.loaded = ObjectA.fake_fun; 如您所见,在您的代码中不能使用ObjectB.loaded = ObjectA.fake_fun; if the fake_fun needs to be evaluated in context of ObjectA . 如果fake_fun需要在上下文中评估ObjectA You need to change it to ObjectB.loaded = function () { ObjectA.loaded(); }; 您需要将其更改为ObjectB.loaded = function () { ObjectA.loaded(); }; ObjectB.loaded = function () { ObjectA.loaded(); };

Ok, as far as I understand you want to add a call back function to the loaded variable of objectB and then call it after some time as this.loaded(); 好的,据我所知,您想在对象B的加载变量中添加一个回调函数,然后在this.loaded()之后再调用它。

Now there can be more than one way of doing that but you want to use it as ObjectB.loaded = fun; 现在可以有多种方法来实现,但是您想将其用作ObjectB.loaded = fun;

and so instead of 所以代替

ObjectA.set_load_fun (ObjectB.loaded);

send 发送

ObjectA.set_load_fun (ObjectB);

and use it as : 并用作:

ObjectA.set_load_fun = function (obj) { obj.loaded = ObjectA.fake_fun;} 

the reason you need to do that is because the if completely change the reference being passed then the original object remains unaffected. 您需要这样做的原因是,如果完全更改了要传递的引用,则原始对象将不受影响。 for more clarity read pass by value and reference in javascript. 为了更清晰,请阅读javascript中按值传递和引用。

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

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