Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
371 views
in Technique[技术] by (71.8m points)

javascript - 为什么null为对象,并且null和undefined有什么区别?(Why is null an object and what's the difference between null and undefined?)

Why is null considered an object in JavaScript?

(为什么在JavaScript中将null视为object ?)

Is checking

(正在检查)

if ( object == null )
      Do something

the same as

(与...相同)

if ( !object )
      Do something

?

(?)

And also:

(并且:)

What is the difference between null and undefined ?

(nullundefined什么区别?)

  ask by rahul translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
(name is undefined)

You: What is name ?

(你: name什么?)

(*)

((*))
JavaScript: name ?

(JavaScript: name ?)

What's a name ?

(叫什么name ?)

I don't know what you're talking about.

(我不知道你在说什么)

You haven't ever mentioned any name before.

(您以前从未提到过任何name 。)

Are you seeing some other scripting language on the (client-)side?

(您是否在(客户端)看到其他脚本语言?)

name = null;

You: What is name ?

(你: name什么?)
JavaScript: I don't know.

(JavaScript:我不知道。)

In short;

(简而言之;)

undefined is where no notion of the thing exists;

(undefined是不存在事物的概念的地方;)

it has no type, and it's never been referenced before in that scope;

(它没有类型,并且在该范围内从未被引用过;)

null is where the thing is known to exist, but it's not known what the value is.

(null是已知的事物存在的地方,但是它的值是未知的。)

One thing to remember is that null is not, conceptually, the same as false or "" or such, even if they equate after type casting, ie

(要记住的一件事是,从概念上讲, nullfalse""等并不相同,即使它们在类型转换之后相等,即)

name = false;

You: What is name ?

(你: name什么?)
JavaScript: Boolean false.

(JavaScript:布尔值false。)

name = '';

You: What is name ?

(你: name什么?)
JavaScript: Empty string

(JavaScript:空字符串)


*: name in this context is meant as a variable which has never been defined.

(*:在此上下文中, name含义是从未定义的变量。)

It could be any undefined variable, however, name is a property of just about any HTML form element.

(它可以是任何未定义的变量,但是name几乎是任何HTML表单元素的属性。)

It goes way, way back and was instituted well before id.

(它沿路前进,早在id之前就已建立。)

It is useful because ids must be unique but names do not have to be.

(这很有用,因为id必须是唯一的,但名称不必是唯一的。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...