Javascript instanceof& GWT中的typeof(JSNI) [英] Javascript instanceof & typeof in GWT (JSNI)

查看:108
本文介绍了Javascript instanceof& GWT中的typeof(JSNI)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试通过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');
}

而我们要调用这个函数的用户JSNI:

  public static native 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

  • 对象,以便它们被正确识别?


解决方案

instanceof 不应该在您的示例中始终返回false,除非是从另一个窗口测试对象,因为来自一个窗口的数组不是

使用 instanceof Array 构造函数的一个实例。 c $ c>当你需要测试一个特定的东西,并且你在一个窗口内操作时(你必须知道scunliffe指出的字符串原始对象和String对象)时,c $ c>非常棒。请注意,您需要小心订单,因为数组是 instanceof Object (以及阵列);这适用于字符串 s以及所有其他对象。



还有一个替代方案没有窗口问题,如果您正在进行调度,可以很容易地用于 switch 语句之类:

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

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

  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 实例。


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?

解决方案

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.

这篇关于Javascript instanceof& GWT中的typeof(JSNI)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆