为什么在 VisualVM (jhat?) oql 中使用 javascript 有时会令人困惑?
我有兴趣知道为什么 VisualVM OQL 对以下语句有问题:
select filter(heap.objects("java.util.HashMap"), isTrue(it));
function isTrue(object) {
return true;
}
例外是:
javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "it" is not defined. (#1)
相反,VisualVM OQL 对以下任何示例都没有问题:
示例 1(注意“它”没有被引用):
select filter(heap.objects("java.util.HashMap"),
function(it) {
return true;
});
示例 2 (注意“it”被引用):
select filter(heap.objects("java.util.HashMap"), isTrue("it"));
function isTrue(object) {
if (object instanceof String) {
throw "String passed!";
}
return true;
}
示例3(“function(it)”由于某种原因在OQL中特别处理?):
select filter(heap.objects("java.util.HashMap"), function(it) { return isTrue(it); });
function isTrue(object) {
return true;
}
我问这个是因为它看起来不直观,并且非直观行为的变化意外地出现并减慢了我的速度下来当我我正在尝试创造一些可用的东西。
I am interested in knowing why visualvm OQL has a problem with the following statement:
select filter(heap.objects("java.util.HashMap"), isTrue(it));
function isTrue(object) {
return true;
}
Exception is:
javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "it" is not defined. (#1)
In contrast, visualvm OQL does not have a problem with any of the following examples:
Example 1 (note "it" is not quoted):
select filter(heap.objects("java.util.HashMap"),
function(it) {
return true;
});
Example 2 (note "it" is quoted):
select filter(heap.objects("java.util.HashMap"), isTrue("it"));
function isTrue(object) {
if (object instanceof String) {
throw "String passed!";
}
return true;
}
Example 3 ("function(it)" handled specially in OQL for some reason?):
select filter(heap.objects("java.util.HashMap"), function(it) { return isTrue(it); });
function isTrue(object) {
return true;
}
I ask this because it seems non-intuitive and variations of non-intuitive behavior show up unexpectedly and slow me down when I am trying to create something usable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的不同语法不等效。第一种:
使用
it
参数调用isTrue
并将其结果传递给filter()
函数。此时您还没有定义变量it
,因此会出现错误。您的“示例 1”和“示例 3”都将一个函数作为第二个参数传递给
filter()
函数。您传入的函数(大概)旨在作为filter()
将调用的回调,其中参数it
将由filter()
设置代码>.您的“示例 2”有点像第一个代码,它立即调用
isTrue("it")
,但在本例中,它使用字符串文字调用它,因此您没有参数未定义的问题。然而,这又是将函数的结果作为参数传递给filter()
而不是传递函数本身。filter()
的正确用法是向其传递一个函数(函数引用 - 不带括号的函数名称 - 或函数表达式如“示例 1”中所示)。所以请尝试以下操作:Your different syntaxes are not equivalent. The first:
calls
isTrue
with a parameter ofit
and passes its result to thefilter()
function. At that point you don't have a variableit
defined, hence the error.Your "example 1" and "example 3" both pass a function as the second parameter to the
filter()
function. The function you pass in is (presumably) intended as a callback thatfilter()
will call, where the parameterit
will be set byfilter()
.Your "example 2" is a bit like the first code in that it calls
isTrue("it")
immediately, but in this case it is calling it with a string literal, so you don't have a problem with the parameter being undefined. However, again this is passing the result of the function as a parameter tofilter()
rather than passing the function itself.Correct usage of
filter()
is to pass it a function (either a function reference - the name of a function without parentheses - or a function expression like in your "example 1"). So try the following: