首页 \ 问答 \ 回调函数javascript(callback function javascript)

回调函数javascript(callback function javascript)

我正在使用MooTools,我遇到了回调函数的问题。 这是一个例子:

function func1(callback){
    var event = 'something';
    callback(event);
}

function func2(data){

}

var Obj = new Class({
    initialize: function(){
        this.data = "data";
        //there I want to use func1 and use func2 as callback and this.data as argument of func2
    }
});

所以我试试

func1(function(){
    func2(this.data);
});

但我不能在匿名函数中使用this.data,因为这不是相同的上下文。

func1(func2)

这不起作用,因为我无法传递this.data作为参数。

这是一个简单的例子,func1来自一个库,所以我无法编辑它。


I'm working with MooTools and I have a problem with a callback function. This is a little exemple:

function func1(callback){
    var event = 'something';
    callback(event);
}

function func2(data){

}

var Obj = new Class({
    initialize: function(){
        this.data = "data";
        //there I want to use func1 and use func2 as callback and this.data as argument of func2
    }
});

So I try

func1(function(){
    func2(this.data);
});

But I can't use this.data in an anonymous function because this is not the same context.

And

func1(func2)

This didn't works because I can't pass this.data as argument.

This is juste a simple exemple, func1 comes from a library so I can't edit it.


原文:https://stackoverflow.com/questions/21262784
更新时间:2024-02-02 06:02

最满意答案

tl; dr看到 两个选择。

上述方法的问题是Target.prototype没有在脚本范围内正确设置。 有关如何在脚本范围内正确定义原型的详细信息,请参阅静态ScriptableObject.defineClass()方法。

您有几个替代方法可以将Target构造函数提供给脚本。 第一种方法是始终为所有脚本定义Target构造函数。 如果您事先知道您希望Target可以在全球范围内使用,那么这很有效。 这基本上归结为以下几点:

final Context context = Context.enter();
try {
  final ScriptableObject scope = context.initStandardObjects();
  ScriptableObject.defineClass(scope, Target.class, false, true);
  context.evaluateString(scope, script, "script", 1, null);
  // etc.
} finally {
  Context.exit();
}

如果您希望脚本作者决定哪些构造函数是必需的,则第二种方法是将defineClass函数提供给脚本。 使用这个函数,脚本作者可以在他们的类路径上“导入”任何脚本对象(这可能比你想要的更多)。 要为脚本提供defineClass函数,请在输入上下文后执行以下操作:

final Context context = Context.enter();
try {
  final ScriptableObject scope = context.initStandardObjects();
  scope.defineFunctionProperties(
          new String[] {"defineClass"},
          Global.class,
          ScriptableObject.DONTENUM);

  context.evaluateString(scope, script, "script", 1, null);
  // etc.
} finally {
  Context.exit();
}

然后,JavaScript作者使用以下Target构造函数:

defineClass("com.acme.rhino.Target");
// whatever `getClassName()` returns is now available
var target = new Target();

在上述两个分支中,我做了一些其他更改,如果将更多内容添加到Target构造函数中,这些更改会使您更好。 零参数构造函数不需要@JSConstructor注释。 如果以后想要一个接受参数的构造函数,这个零参数构造函数将用作原型构造函数,并且您可以在将用于初始化对象的方法上使用@JSConstructor注释。 根据您创建此构造方法的方式,在JavaScript中使用new关键字将变得非常重要。

简而言之, Packages.com.acme...语法对于从脚本访问ScriptableObject构造函数没有用处。


tl;dr see these two alternatives.

The problem with the approach above is that Target.prototype is not properly set up in the script scope. See the static ScriptableObject.defineClass() method for details on how to properly define prototypes in a script scope.

You have a couple alternatives for providing the Target constructor to your scripts. The first alternative would be to always define the Target constructor for all scripts. This works well if you know ahead of time that you want Target to be globally available. This basically comes down to the following:

final Context context = Context.enter();
try {
  final ScriptableObject scope = context.initStandardObjects();
  ScriptableObject.defineClass(scope, Target.class, false, true);
  context.evaluateString(scope, script, "script", 1, null);
  // etc.
} finally {
  Context.exit();
}

If instead you want the script author to decide which constructors are necessary, the second alternative is to provide the defineClass function to scripts. With this function, script authors can "import" any scriptable objects on their class path (which may be more than you want to allow). To provide the defineClass functions to scripts, do the following after entering the context:

final Context context = Context.enter();
try {
  final ScriptableObject scope = context.initStandardObjects();
  scope.defineFunctionProperties(
          new String[] {"defineClass"},
          Global.class,
          ScriptableObject.DONTENUM);

  context.evaluateString(scope, script, "script", 1, null);
  // etc.
} finally {
  Context.exit();
}

And then, the JavaScript author makes use of the Target constructor with the following:

defineClass("com.acme.rhino.Target");
// whatever `getClassName()` returns is now available
var target = new Target();

In both of the above branches, I've made a couple other changes that set you up better if you add more to the Target constructor. The zero argument constructor doesn't need the @JSConstructor annotation. If you later want to have a constructor that accepts arguments, this zero argument constructor will be used as the prototype constructor, and you can use the @JSConstructor annotation on a method that will be used to initialize your object. Depending on how you author this constructor method, it will become important to use the new keyword in your JavaScript.

In short, the Packages.com.acme... syntax is not useful for getting access to ScriptableObject constructors from scripts.

相关问答

更多
  • 我不确定如何检查某些东西是不是未定义的,同时得到一个未定义的错误。 你使用的是什么浏览器? 你可以通过以下方式进行检查(额外的=和长度评估) if (typeof(sub.from) !== 'undefined' && sub.from.length) { [更新] 我看到你重置sub,从而重置sub.from但未能重新检查sub.from是否存在: for (var i = 0; i < sub.from.length; i++) {//<== assuming sub.from.exist ...
  • tl; dr看到这 两个选择。 上述方法的问题是Target.prototype没有在脚本范围内正确设置。 有关如何在脚本范围内正确定义原型的详细信息,请参阅静态ScriptableObject.defineClass()方法。 您有几个替代方法可以将Target构造函数提供给脚本。 第一种方法是始终为所有脚本定义Target构造函数。 如果您事先知道您希望Target可以在全球范围内使用,那么这很有效。 这基本上归结为以下几点: final Context context = Context.enter( ...
  • 这是正确的,完成rending后会发生不完整。 以下是关于a4j的更多信息:jsFunction: http ://mkblog.exadel.com/ria/richfaces-ria/using-richfaces-a4jjsfunction-sending-an-ajax-request-from-any-javascript/。 That's correct, oncomplete happens after rending is done. Here is more info on a4j:jsF ...
  • 你迭代太多次了。 在for循环中替换<= by < 。 你正在迭代3次。 第三次i等于2.但是索引2超出了你的数组的范围。 for( var i = 0; i < p.items.length; i++ ){ } You are iterating one too many times. Replace <= by < in your for loop. You are iterating 3 times. The third time i equals 2. But index 2 is out of ...
  • 你的variantAlert使用like variantAlert.message('failed'); 这意味着构造函数必须返回包含消息函数的对象 var VariantAlert = function (form) { var timer; /********************************* * handles showing/hiding any messages * in the variant forms ****************************** ...
  • 你误解了的用途。 它自动生成一个JavaScript函数,然后您可以从视图中的任何JavaScript代码中调用该函数。 你的例子, 将自动生成以下功能