Javascript实例& GWT 中的 typeof (JSNI)

发布于 2024-09-03 06:28:00 字数 718 浏览 12 评论 0原文

我在尝试通过 GWT 中的 JSNI 使用某些对象时遇到了一个奇怪的问题。假设我们有一个定义了函数的 javscript 文件:

test.js:

function test(arg){
  var type = typeof(arg);
  if (arg instanceof Array)
    alert('Array');
  if (arg instanceof Object)
    alert('Object');
  if (arg instanceof String)
    alert('String');
}

我们想要调用这个函数 user JSNI:

public static native void testx()/ *-{
  $wnd.test( new Array(1, 2, 3) );
  $wnd.test( [ 1, 2, 3 ] );
  $wnd.test( {val:1} );
  $wnd.test( new String("Some text") );
}-*/;

问题是:

  • 为什么 instanceof 指令总是返回 false
  • 为什么 typeof 总是返回 "object"
  • 如何传递这些对象以便它们被正确识别?

I've encountered an curious problem while trying to use some objects through JSNI in GWT. Let's say we have javscript file with the function defined:

test.js:

function test(arg){
  var type = typeof(arg);
  if (arg instanceof Array)
    alert('Array');
  if (arg instanceof Object)
    alert('Object');
  if (arg instanceof String)
    alert('String');
}

And the we want to call this function user JSNI:

public static native void testx()/ *-{
  $wnd.test( new Array(1, 2, 3) );
  $wnd.test( [ 1, 2, 3 ] );
  $wnd.test( {val:1} );
  $wnd.test( new String("Some text") );
}-*/;

The questions are:

  • why instanceof instructions will always return false?
  • why typeof will always return "object" ?
  • how to pass these objects so that they were recognized properly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情泪▽动烟 2024-09-10 06:28:00

instanceof 在您的示例中不应始终返回 false 除非您正在从不同的窗口测试对象,因为来自一个窗口的数组不是< /em> 不同窗口的 Array 构造函数的实例。

当您需要测试特定事物并且在一个窗口中进行操作时,使用instanceof 非常有用(您必须注意 scunliffe 指出的字符串原语与字符串对象)。请注意,您需要注意顺序,因为数组是一个 instanceof Object (以及 Array);这也适用于String和所有其他对象。

有一种替代方案不存在窗口问题,并且如果您正在执行调度,则可以很容易地将其用于 switch 语句等:

function classify(arg) {
    return Object.prototype.toString.call(arg);
}

这看起来很奇怪,但它的作用是使用 Object 原型上的 >toString 函数,该函数具有已定义的行为(而不是使用您正在测试的实际对象可能具有的任何覆盖,这可能具有不同的行为)。因此,给定这个函数:

function show(arg) {
    alert(classify(arg));
}

您将得到这些结果:

show({});               // [object Object]
show("a");              // [object String]
show(new String("a"));  // [object String]
show([]);               // [object Array]
show(/n/);              // [object RegExp]
show(function() { });   // [object Function]

并且无论您正在测试的对象来自哪个窗口,也无论您使用字符串原语还是String 实例。

instanceof shouldn't be returning false all the time in your example unless you're testing objects from a different window, because an array from one window is not an instance of the Array constructor of a different window.

Using instanceof is great when you need to test for a specific thing and you're operating within one window (you do have to be aware of the string primitive vs. String object thing that scunliffe pointed out). Note that you need to be careful of your order, since an array is an instanceof Object (as well as Array); this applies to Strings and all other objects as well.

There's an alternative that doesn't have the window issue and which can readily be used for switch statements and the like if you're doing dispatch:

function classify(arg) {
    return Object.prototype.toString.call(arg);
}

That looks odd, but what it does is use the toString function on the Object prototype, which has a defined behavior (rather than using any override that the actual object you're testing may have, which may have different behavior). So given this function:

function show(arg) {
    alert(classify(arg));
}

you'll get these results:

show({});               // [object Object]
show("a");              // [object String]
show(new String("a"));  // [object String]
show([]);               // [object Array]
show(/n/);              // [object RegExp]
show(function() { });   // [object Function]

and you'll get those results regardless of what window the object you're testing is coming from and regardless of whether you use a string primitive or a String instance.

冷月断魂刀 2024-09-10 06:28:00

由于其他所有问题似乎都已经得到解答,让我得到这个:

如何传递这些对象以便正确识别它们?

GWT 会自动对字符串、整数等基本类型执行此操作。因此您只需编写:

public static native String test1()/ *-{
   return "adfasdf";
}-*/;

public static native int test2()/ *-{
   return 23;
}-*/;

请参阅 文档以获得一些额外的注释。

对于数组,有很多包装类:JsArrayJsArrayBooleanJsArrayInteger, JsArrayNumber, JsArrayString

public static native JsArrayString test3()/ *-{
   return ['foo', 'bar', 'baz'];
}-*/;

Since everything else seem to have been answered let me get this one:

how to pass these objects so that they were recognized properly?

GWT does this automagically for primitive types like Strings, Integers, etc. So you can write just:

public static native String test1()/ *-{
   return "adfasdf";
}-*/;

public static native int test2()/ *-{
   return 23;
}-*/;

See the docs for some extra notes.

For arrays, there are a bunch of wrapper classes: JsArray, JsArrayBoolean, JsArrayInteger, JsArrayNumber, JsArrayString.

public static native JsArrayString test3()/ *-{
   return ['foo', 'bar', 'baz'];
}-*/;
梦里°也失望 2024-09-10 06:28:00

你的测试函数总是返回 false,因为你没有提供 return 语句...并且 String 在 JavaScript 中很有趣...如果你使用 new String("asdf"); 那么使用instanceof就可以了,如果你只是用"asdf"创建一个字符串,那么你将需要使用typeof。

function test(arg){
  if (arg instanceof Array){
    return 'Array';
  } else if(arg instanceof String || typeof(arg) == 'String'){
    return 'String';
  } else if (arg instanceof Object){
    return 'Object';
  } else {
    return typeof(arg);
  }
}

(注意还有其他类型......日期、数字、自定义对象等)

your test function always returns false because you do not supply a return statement... and String is funny in JavaScript... if you use new String("asdf"); then using instanceof will work, if you just create a string with "asdf" then you will need to use typeof.

function test(arg){
  if (arg instanceof Array){
    return 'Array';
  } else if(arg instanceof String || typeof(arg) == 'String'){
    return 'String';
  } else if (arg instanceof Object){
    return 'Object';
  } else {
    return typeof(arg);
  }
}

(note there are other types... Date, Number, Custom Objects etc.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文