本文介绍了jslint不喜欢我的条件对象创建语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我一直在使用以下代码行创建一个名为

" Serious"如果它还没有存在。


if(严重== null){var Serious = {};}


这是有效的在我使用它的脚本中但是不满意

和我在一起。


第1行问题16:使用''===''与''null''进行比较。

if(严重== null){var Serious = {};}


第1行第27个问题:Var Serious在它之前使用

是声明。

if(严重== null){var Serious = {};}


第1行问题27:标识符''严重''已经

宣布为全球

if(严重== null){var Serious = {};}


如果我改变到===然后条件永远不会真实。


只有当对象

不存在时,创建空对象的正确方法是什么?


谢谢,

彼得

解决方案




没有任何''正确''测试,除了测试告诉你

正是你想知道的,这取决于你想要什么

知道。


如果声明的全局变量的唯一可能性是

未定义的值或对象的引用(没有别的)那么

类型转换(对布尔值)测试对于前者是假的,对于后者是真正的

: -


if(Serious) {

... //严重未定义(因此是对象参考)

}


- 或: -


如果(!严重){

... //严重未定义d(因此不是对象参考)

}


效率较低的测试,例如使用 - typeof - 可以更多

辨别: -


if(严重= ='''未定义''){

... //严重未定义。

}


- 如果可能性不太明确,这可能会有优势。


Richard。




JSLint是对的,你的代码是错误的,但在这种情况下它会咆哮

错误的树。除非在try-catch块或typeof运算符中使用
,否则无法处理非初始化变量。


if(Serious == null)

为了检查此语句引擎需要计算严重的

值。如果严重不存在,我们将尝试

来访问非初始化变量,这就是故事的结尾。

试试这个以查看它行动:


<!DOCTYPE HTML PUBLIC" - // W3C // DTD HTML 4.01 // EN">

< html>

< head>

< title>



Hi,

I have been using the following line of code to create an object called
"Serious" if it doesn''t already exist.

if (Serious == null) {var Serious = {};}

This works in the scripts I use it but but www.jslint.com is not happy
with me.

Problem at line 1 character 16: Use ''==='' to compare with ''null''.
if (Serious == null) {var Serious = {};}

Problem at line 1 character 27: Var Serious was used before it
was declared.
if (Serious == null) {var Serious = {};}

Problem at line 1 character 27: Identifier ''Serious'' already
declared as global
if (Serious == null) {var Serious = {};}

If I change to "===" then the condition is never true.

What is the correct way to create an empty object only if the object
does not exist?

Thanks,
Peter

解决方案



There is not really any ''correct'' test, except the test that tells you
precisely what you want to know, which depends upon what you want to
know.

If the only possibilities for a declared global variable are the
undefined value or a reference to an object (and nothing else) then a
type-converting (to boolean) test will be false for the former and true
for the latter:-

if(Serious){
... //Serious is not undefined (therefor is an object reference)
}

- or:-

if(!Serious){
... //Serious is undefined (therefor is not an object reference)
}

Less efficient tests such as using - typeof - can be more
discriminating:-

if(typeof Serious == ''undefined''){
... //Serious is undefined.
}

- which can have advantages if the possibilities are less well defined.

Richard.




JSLint is right what your code is wrong, but in this case it barks up
the wrong tree. You can not address a non-initialized variable unless
in try-catch block or in typeof operator.

if (Serious == null)
in order to check this statement the engine needs to calculate the
value of Serious. If Serious doesn''r exists, we are getting an attempt
to access non-initialized variable and that is the end of the story.
Try this to see it in action:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title>



这篇关于jslint不喜欢我的条件对象创建语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 21:55