jslint不喜欢我的条件对象创建语法 [英] jslint doesn't like my syntax for conditional object creation

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

问题描述




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

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


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


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

和我在一起。


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

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


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

是声明。

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


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

宣布为全球

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


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


只有当对象

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


谢谢,

彼得

解决方案

pe ********** @ gmail.com 写道:

我一直在使用以下代码行创建一个名为Serious的对象。如果它还没有存在。

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

这在我使用它的脚本中有效,但是 www.jslint.com 对我不满意。
<第1行第16行的问题:使用''===''与
''null''进行比较。 if(严重== null){var Serious = {};}


双等于比较是类型转换,并将未定义

等于Null。 JSLint似乎采取的立场是,如果你打算将
与null进行比较,那么你应该只对

感兴趣,无论它是否为空,所以应该使用一个比较,电视你

正是如此。

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

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


由于范围单位是函数体,因此在

a函数体内声明的任何变量都可见函数的全部内容,以及函数的直接内容中任何位置的
变量声明

(而不是嵌套函数)导致创建一个名为

属性的Variable对象_prior_执行任何函数

正文代码。


这个包含: -


如果(严重== null){var Serious = {};}


- 其行为与以下内容没有区别: -


var严重;

如果(严重== null){严重= {};}


- 并给予(和特别是所有声明的变量

导致在函数之前创建Variable对象属性的事实

体代码执行)通常认为所有函数局部变量

声明应该出现在函数体的代码顶部。

第1行问题27:标识符''严重''
已经声明为全局
if(严重== null){var Serious = {};}


声明一个函数局部变量将屏蔽全局变量,以便

他们无法在函数体内访问。但是,这可能是必要的或者不可取的,所以可能会觉得如果全局声明一个

变量,那么任何使用相同的变量名称/>
本地变量应标记为潜在错误。

如果我改为===那条件永远不会成真。


不,未分配的局部变量将具有Undefined值,因此

类型转换比较将等于null与undefined strict

比较不会。

只有在对象不存在的情况下创建空对象的正确方法是什么?




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

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

知道。


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

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

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

: -


if(Serious) {

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

}


- 或: -


如果(!严重){

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

}


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

辨别: -


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

... //严重未定义。

}


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


Richard。



pe ********** @ gmail。 com 写道:

我一直在使用以下代码行创建一个名为
Serious的对象。如果它还没有存在。

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

这在我使用它的脚本中有效,但是 www.jslint.com 对我不满意。
<第1行第16行的问题:使用''===''与''null''进行比较。
if(Serious == null){var Serious = {};}




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

解决方案

pe**********@gmail.com wrote:

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 = {};}
The double equals comparison is type-converting, and equates Undefined
with Null. JSLint seems to take the stance that if you are going to
compare something with null then you should only be interested in
whether it is null or not, and so should use a comparison that telly you
precisely that.
Problem at line 1 character 27: Var Serious was used
before it was declared.
if (Serious == null) {var Serious = {};}
As the units of scoping are function bodies any variable declared within
a function body is visible to the function''s entire contents, and
variable declarations anywhere within a function''s immediate contents
(rather than nested functions) result in the creation of a named
property of the Variable object _prior_ to the execution of any function
body code.

This mans that:-

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

- is no different in its behaviour from:-

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

- and given that (and particularly the fact that all declared variable
result in the creation of Variable object properties prior to function
body code execution) it is often felt that all function local variable
declarations should appear at the top of a function body''s code.
Problem at line 1 character 27: Identifier ''Serious''
already declared as global
if (Serious == null) {var Serious = {};}
Declaring a function local variable will mask global variables so that
they cannot be accessed within the function body. However, this is
unlikely to be necessary or desirable, so it may be felt that if a
variable is declared globally any use of the same variable name for a
local variable should be flagged as a potential error.
If I change to "===" then the condition is never true.
No, the unassigned local variable will have the Undefined value so while
type-converting comparison will equate null with undefined strict
comparison will not.
What is the correct way to create an empty object only if
the object does not exist?



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.



pe**********@gmail.com wrote:

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 = {};}



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>


Template


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

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