为什么null在JavaScript中被认为是一个对象?
正在检查
if ( object == null )
Do something
和
if ( !object )
Do something
?
还有:
null和undefined之间的区别是什么?
为什么null在JavaScript中被认为是一个对象?
正在检查
if ( object == null )
Do something
和
if ( !object )
Do something
?
还有:
null和undefined之间的区别是什么?
当前回答
摘自Nicholas C. Zakas的《面向对象的Javascript原理》
但是为什么对象类型是空的呢?(事实上,TC39(设计和维护JavaScript的委员会)已经承认这是一个错误。你可以推断null是一个空对象指针,使“object”成为一个逻辑返回值,但这仍然令人困惑。)
尼古拉斯·扎卡斯(2014-02-07)。面向对象JavaScript的原则(Kindle位置226-227)。没有淀粉机。Kindle版。
也就是说:
var game = null; //typeof(game) is "object"
game.score = 100;//null is not an object, what the heck!?
game instanceof Object; //false, so it's not an instance but it's type is object
//let's make this primitive variable an object;
game = {};
typeof(game);//it is an object
game instanceof Object; //true, yay!!!
game.score = 100;
未定义的例子:
var score; //at this point 'score' is undefined
typeof(score); //'undefined'
var score.player = "felix"; //'undefined' is not an object
score instanceof Object; //false, oh I already knew that.
其他回答
Null不是一个对象,它是一个基本值。例如,不能向其添加属性。有时人们错误地认为它是一个对象,因为typeof null返回“object”。但这实际上是一个错误(甚至可能在ECMAScript 6中被修复)。
null和undefined的区别如下:
undefined: used by JavaScript and means “no value”. Uninitialized variables, missing parameters and unknown variables have that value. > var noValueYet; > console.log(noValueYet); undefined > function foo(x) { console.log(x) } > foo() undefined > var obj = {}; > console.log(obj.unknownProperty) undefined Accessing unknown variables, however, produces an exception: > unknownVariable ReferenceError: unknownVariable is not defined null: used by programmers to indicate “no value”, e.g. as a parameter to a function.
检查变量:
console.log(typeof unknownVariable === "undefined"); // true
var foo;
console.log(typeof foo === "undefined"); // true
console.log(foo === undefined); // true
var bar = null;
console.log(bar === null); // true
作为一般规则,在JavaScript中应该总是使用===,而永远不要使用==(==执行各种可能产生意外结果的转换)。检查x == null是一个边缘情况,因为它适用于null和undefined:
> null == null
true
> undefined == null
true
检查变量是否有值的一种常见方法是将其转换为布尔值,然后看它是否为真。该转换由if语句和布尔运算符!(“不”)。
function foo(param) {
if (param) {
// ...
}
}
function foo(param) {
if (! param) param = "abc";
}
function foo(param) {
// || returns first operand that can't be converted to false
param = param || "abc";
}
这种方法的缺点:以下所有值的计算结果都是false,所以你必须小心(例如,上面的检查不能区分undefined和0)。
定义,零 布尔值:假 数字:+0,-0,NaN 弦:“”
你可以通过使用boolean作为函数来测试到boolean的转换(通常它是一个构造函数,用于new):
> Boolean(null)
false
> Boolean("")
false
> Boolean(3-3)
false
> Boolean({})
true
> Boolean([])
true
Null是一个对象。它的类型是null。Undefined不是一个对象;其类型是未定义的。
思考“null”的最佳方式是回忆一下类似的概念是如何在数据库中使用的,在数据库中它表示字段包含“根本没有值”。
是的,物品的价值是已知的;它是“被定义的”。已经初始化。 该项的值是:“没有值。”
This is a very useful technique for writing programs that are more-easily debugged. An 'undefined' variable might be the result of a bug ... (how would you know?) ... but if the variable contains the value 'null,' you know that "someone, somewhere in this program, set it to 'null.'" Therefore, I suggest that, when you need to get rid of the value of a variable, don't "delete" ... set it to 'null.' The old value will be orphaned and soon will be garbage-collected; the new value is, "there is no value (now)." In both cases, the variable's state is certain: "it obviously, deliberately, got that way."
null和undefined之间的区别是什么??
没有定义的属性是未定义的。null是一个对象。它的类型是object。Null是一个特殊值,表示“没有值”。Undefined不是一个对象,它的类型是Undefined。
你可以声明一个变量,将其设置为null,除了你会看到“null”和“undefined”打印出来之外,行为是相同的。你甚至可以将一个未定义的变量与null进行比较,反之亦然,条件将为真:
undefined == null
null == undefined
更多细节请参考JavaScript null和undefined之间的差异。
还有你的新编辑,是的
if (object == null) does mean the same if(!object)
当测试object是否为false时,它们都只满足测试是否为false时的条件,而不满足测试是否为true时的条件
检查这里:Javascript抓住你了
例如窗口。someWeirdProperty是未定义的
”窗口。someWeirdProperty === null"的值为false时
”窗口。someWeirdProperty === undefined"的值为true。
此外,checkif if (!o)与检查if (o == null)是否为假并不相同。