简体   繁体   English

“var 变量”返回未定义?

[英]“var variable” returns undefined?

When I run "var variable = true;"当我运行“var variable = true;” in chrome console I get "undefined" returned:在 chrome 控制台中,我得到“未定义”的返回:

> var variable = true;
undefined

But when I run without "var" it returns true:但是当我在没有“var”的情况下运行时,它返回true:

> variable = true;
true

Why is it returning "undefined" with "var"?为什么用“var”返回“undefined”?

It's confusing cause I expected it would return true.这令人困惑,因为我预计它会返回 true。

The first is a statement, while the second is an expression.第一个是语句,第二个是表达式。 While not quite the same, it is similar to C's rules:虽然不完全相同,但它类似于 C 的规则:

// A statement that has no value.
int x = 5;

// An expression...
x = 10;

// ...that can be passed around.
printf("%d\n", x = 15);

var x = y; is a statement which returns no value.是一个不返回任何值的语句 In the WebKit JS console, a statement that returns no value will show undefined as the result, eg在 WebKit JS 控制台中,没有返回值的语句将显示undefined作为结果,例如

> if(1){}
undefined
> ;
undefined
> if(1){4}  // this statement returns values!
4

The assignment is an expression which returns the value of the LHS.赋值是一个返回 LHS 值的表达式 That means, this expression statement has a return value, and this will be shown.这意味着,这个表达式语句有一个返回值,这将被显示。

一个赋值返回赋值的值,但是对于var这个返回是“消耗”的(?)

Statements always return undefined .语句总是返回undefined

var color = "red"; 
undefined

Expressions always return a value.表达式总是返回一个值。

color = "orange";
"orange"

I'd like to point out, that the answer provided by kennytm should be the accepted answer, at least for pedagogical purposes.我想指出, kennytm提供的答案应该是公认的答案,至少出于教学目的。 The accepted answer doesn't answer why this is the case or provide deeper understanding.接受的答案没有回答为什么会这样或提供更深入的理解。

Like the null value in JavaScript, undefined indicates absence of value, but in a much deeper way.就像 JavaScript 中的null值一样, undefined表示没有值,但以更深层次的方式表示。 The following should be taken as complimentary to the above-mentioned answers:以下应视为对上述答案的补充:

undefined is the value of variables that haven't been initialized and the value you get when you query the value of an object property or array element that doesn't exist. undefined是尚未初始化的变量的值,以及在查询不存在的对象属性或数组元素的值时获得的值。 This value is also returned by functions that have no return value, and the value of function parameters for which no argument is supplied.这个值也由没有返回值的函数返回,以及没有提供参数的函数参数的值。 undefined is a predefined global variable (not a language keyword like null) that is initialized to the undefined value. undefined是一个预定义的全局变量(不是像 null 这样的语言关键字),它被初始化为 undefined 值。

You might consider undefined to represent a system-level, unexpected, or error-like absence of value and null to represent program-level, normal, or expected absence of value.您可能认为undefined表示系统级、意外或类似错误的值缺失,而null表示程序级、正常或预期的值缺失。

-- Flanagan, David. ——弗拉纳根,大卫。 JavaScript: The Definitive Guide: Activate Your Web Pages (Definitive Guides) . JavaScript:权威指南:激活您的网页(权威指南)。 O'Reilly Media.奥莱利媒体。 Kindle Edition.点燃版。

Also, makes sure to check out both the accepted and the second most voted answer for further reference: Chrome/Firefox console.log always appends a line saying undefined此外,请务必查看已接受和投票第二多的答案以供进一步参考: Chrome/Firefox console.log 总是附加一行表示未定义

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM