本文介绍了DukeScript:对JavaScript的本地调用如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力了解DukeScript中的本机方法"调用是如何工作的.特别是,@ JavascriptBody批注中未指定任何主体的主体. 例如:

I'm struggling to understand how "native method" calls in DukeScript work.In particular, the ones where no body is specified in the @JavascriptBody annotation. For example:

@JavaScriptResource(value = "userEntryComponent.js")
public final class UserEntryWidget {

    private UserEntryWidget() {
    }

    @JavaScriptBody(args = {}, body = "")
    public static native void registerComponent();
}

"registerComponent()"方法在哪里定义?在淘汰赛中,有一个名为"ko.components.register"的javascript函数.因此,"registerComponent"必须是围绕"ko.components.register"的包装.

Where is the "registerComponent()" method defined?In knockout there's a javascript function called "ko.components.register".So "registerComponent" must be a sort of wrapper around "ko.components.register".

没有主体的本机方法调用的另一个示例是此处:

Another example of a native method call without body is here:

@JavaScriptResource("jquery-1.11.0.min.js")
public class JQuery {

    @JavaScriptBody(args = {},body="")
    public static native void init();   
}

那么,在这种情况下,"init()"是什么?是Java方法还是JavaScript函数?

So, in this case, what's "init()"? is it a Java method or a JavaScript function?

推荐答案

我完全理解为什么代码看起来很神奇.但是,如果尝试注释掉 init 方法,则在 javac 并发症期间应该会看到错误:

I fully understand why the code looks magical. However if you try to comment out the init method, you should see error during javac complication:

COMPILATION ERROR : 
-------------------------------------------------------------
JQuery.java:[10,8] At least one method needs @JavaScriptBody
annotation. Otherwise it is not guaranteed the resource will
ever be loaded

错误行是使用 @JavaScriptResource 的行. init 方法定义实际上为空,不执行任何操作.但是一旦调用,它就会强制加载 @JavaScriptResource 中定义的资源.

The error line is the line with @JavaScriptResource usage. The init method definition is really empty and does nothing. But once called, it enforces load of resource defined in @JavaScriptResource.

在剔除情况下, ko.components.register knockout.js 资源文件定义.方法名称​​ registerComponent 可以是任意的,可以在其中触发 knockout.js 资源的加载.

In the knockout case, the ko.components.register is defined by the knockout.js resource file. The method name registerComponent can be arbitrary, it is there to just trigger the load of the knockout.js resource.

这篇关于DukeScript:对JavaScript的本地调用如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 11:27