I want to boolean to come out of this expression
我希望boolean来自这个表达式
(task === undefined);
where task
is arbitrary and doesn’t appear in the code at all.
任务是任意的,根本不会出现在代码中。
However, when I run this in rhino, I get a reference Error. I WANT TRUE
但是,当我在rhino中运行它时,我得到一个引用错误。我想要的
Why don’t I get true?
为什么我不成真?
I want to check if a particular variable has been defined. How do I do it then if this doesn't work?
我想检查是否已定义特定变量。如果这不起作用,我该怎么办呢?
2 个解决方案
#1
55
Use this:
(typeof task === "undefined")
When you use (task === undefined)
, Javascript needs to find the value of task
to see if it is the same as undefined
, but it can't look up the name because it doesn't exist, giving you the reference error. typeof
is special in that it can safely return the type of a name that doesn't exist.
当你使用(task === undefined)时,Javascript需要找到task的值来查看它是否与undefined相同,但它不能查找名称,因为它不存在,给你引用错误。 typeof的特殊之处在于它可以安全地返回不存在的名称的类型。
#2
8
Addendum to the accepted answer to understand why it doesn't work with some examples you can try yourself in a javascript console.
对于理解为什么它不适用于某些示例的已接受答案的补充,您可以在javascript控制台中尝试自己。
Comparing directly with undefined type only works if the variable exist. Below is the output you'll get from the Google Chrome browser:
直接与未定义类型进行比较仅在变量存在时才有效。以下是您从Google Chrome浏览器中获得的输出:
> task === undefined
ReferenceError: task is not defined
However if the variable exists it will work:
但是,如果变量存在,它将起作用:
// continued from above
> var task
undefined
> task === undefined
true
This is the reason why you should use typeof
solution instead because it will work in all cases without throwing errors (and breaking the execution of javascript code).
这就是你应该使用typeof解决方案的原因,因为它可以在所有情况下工作而不会抛出错误(并打破javascript代码的执行)。
// continued from above
> typeof notavariable === 'undefined'
true
> typeof task === 'undefined'
true
Note that you don't need the typeof
check in some cases, such as the properties in a object literal:
请注意,在某些情况下您不需要typeof检查,例如对象文字中的属性:
// continued from above
> var obj = {}
undefined
> obj.test === undefined
true
> obj.test = 1
1
> obj.test === undefined
false
This is because properties in an object behave more like values in an associative array:
这是因为对象中的属性更像是关联数组中的值:
// continued from above
> obj["test"]
1
> obj["test"] === undefined
false
However you can't always be sure this is a case in a function where you have no control over the argument input:
但是,在无法控制参数输入的函数中,您无法始终确定这是一种情况:
// continued from above
> function TestFunc(arg1) { console.log(arg1) }
undefined
> TestFunc(notavariable)
ReferenceError: notavariable is not defined
> TestFunc(task)
undefined
undefined
> TestFunc(obj["lol"])
undefined
undefined
Hope this exercise helps you out to understand the why's of this comparison.
希望这个练习可以帮助你理解这种比较的原因。
#1
55
Use this:
(typeof task === "undefined")
When you use (task === undefined)
, Javascript needs to find the value of task
to see if it is the same as undefined
, but it can't look up the name because it doesn't exist, giving you the reference error. typeof
is special in that it can safely return the type of a name that doesn't exist.
当你使用(task === undefined)时,Javascript需要找到task的值来查看它是否与undefined相同,但它不能查找名称,因为它不存在,给你引用错误。 typeof的特殊之处在于它可以安全地返回不存在的名称的类型。
#2
8
Addendum to the accepted answer to understand why it doesn't work with some examples you can try yourself in a javascript console.
对于理解为什么它不适用于某些示例的已接受答案的补充,您可以在javascript控制台中尝试自己。
Comparing directly with undefined type only works if the variable exist. Below is the output you'll get from the Google Chrome browser:
直接与未定义类型进行比较仅在变量存在时才有效。以下是您从Google Chrome浏览器中获得的输出:
> task === undefined
ReferenceError: task is not defined
However if the variable exists it will work:
但是,如果变量存在,它将起作用:
// continued from above
> var task
undefined
> task === undefined
true
This is the reason why you should use typeof
solution instead because it will work in all cases without throwing errors (and breaking the execution of javascript code).
这就是你应该使用typeof解决方案的原因,因为它可以在所有情况下工作而不会抛出错误(并打破javascript代码的执行)。
// continued from above
> typeof notavariable === 'undefined'
true
> typeof task === 'undefined'
true
Note that you don't need the typeof
check in some cases, such as the properties in a object literal:
请注意,在某些情况下您不需要typeof检查,例如对象文字中的属性:
// continued from above
> var obj = {}
undefined
> obj.test === undefined
true
> obj.test = 1
1
> obj.test === undefined
false
This is because properties in an object behave more like values in an associative array:
这是因为对象中的属性更像是关联数组中的值:
// continued from above
> obj["test"]
1
> obj["test"] === undefined
false
However you can't always be sure this is a case in a function where you have no control over the argument input:
但是,在无法控制参数输入的函数中,您无法始终确定这是一种情况:
// continued from above
> function TestFunc(arg1) { console.log(arg1) }
undefined
> TestFunc(notavariable)
ReferenceError: notavariable is not defined
> TestFunc(task)
undefined
undefined
> TestFunc(obj["lol"])
undefined
undefined
Hope this exercise helps you out to understand the why's of this comparison.
希望这个练习可以帮助你理解这种比较的原因。