Javascript实例& 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');
}
我们想要调用这个函数 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 returnfalse
? - why
typeof
will always return"object"
? - how to pass these objects so that they were recognized properly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
instanceof
在您的示例中不应始终返回 false 除非您正在从不同的窗口测试对象,因为来自一个窗口的数组不是< /em> 不同窗口的Array
构造函数的实例。当您需要测试特定事物并且在一个窗口中进行操作时,使用instanceof 非常有用(您必须注意 scunliffe 指出的字符串原语与字符串对象)。请注意,您需要注意顺序,因为数组是一个
instanceof
Object
(以及Array
);这也适用于String
和所有其他对象。有一种替代方案不存在窗口问题,并且如果您正在执行调度,则可以很容易地将其用于
switch
语句等:这看起来很奇怪,但它的作用是使用
函数,该函数具有已定义的行为(而不是使用您正在测试的实际对象可能具有的任何覆盖,这可能具有不同的行为)。因此,给定这个函数:Object
原型上的 >toString您将得到这些结果:
并且无论您正在测试的对象来自哪个窗口,也无论您使用字符串原语还是
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 theArray
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 aninstanceof
Object
(as well asArray
); this applies toString
s 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:That looks odd, but what it does is use the
toString
function on theObject
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:you'll get these results:
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.由于其他所有问题似乎都已经得到解答,让我得到这个:
GWT 会自动对字符串、整数等基本类型执行此操作。因此您只需编写:
请参阅 文档以获得一些额外的注释。
对于数组,有很多包装类:
JsArray
、JsArrayBoolean
,JsArrayInteger
,JsArrayNumber
,JsArrayString
。Since everything else seem to have been answered let me get this one:
GWT does this automagically for primitive types like Strings, Integers, etc. So you can write just:
See the docs for some extra notes.
For arrays, there are a bunch of wrapper classes:
JsArray
,JsArrayBoolean
,JsArrayInteger
,JsArrayNumber
,JsArrayString
.你的测试函数总是返回 false,因为你没有提供 return 语句...并且
String
在 JavaScript 中很有趣...如果你使用new String("asdf");
那么使用instanceof就可以了,如果你只是用"asdf"
创建一个字符串,那么你将需要使用typeof。(注意还有其他类型......日期、数字、自定义对象等)
your test function always returns false because you do not supply a return statement... and
String
is funny in JavaScript... if you usenew String("asdf");
then using instanceof will work, if you just create a string with"asdf"
then you will need to use typeof.(note there are other types... Date, Number, Custom Objects etc.)