值(Values)

  • 版本网址缩略名: Core_JavaScript_1.5_Guide/值(Values)
  • 版本标题: 值(Values)
  • 版本 id: 282298
  • 创建于:
  • 创建者: Oatn
  • 是否是当前版本?
  • 评论 /* Values */

修订内容

Values

Javascript识别以下的值类型:

  • Numbers, 如 42 或3.14159
  • Logical (Boolean) values, 可以为true 或者 false
  • Strings, 比如 "Howdy!"
  • null, 一个特殊的空字符; null 也是一个初始值. 因为Javascript区分大小写, null 不同于Null, NULL, 或者其他的变量
  • undefined, 值没有被定义的高级属性; undefined 也是一个初始值.

This relatively small set of types of values, or data types, enables you to perform useful functions with your applications. There is no explicit distinction between integer and real-valued numbers. Nor is there an explicit date data type in JavaScript. However, you can use the Date object and its methods to handle dates. Objects and functions are the other fundamental elements in the language. You can think of objects as named containers for values, and functions as procedures that your application can perform.

类型转换

JavaScript 是一个弱类型的语言。这意味着你在声明变量的时候不需要指定数据类型,程序在执行的过程中数据的类型会根据需要自动的转换。例如:你先声明了下面这个变量:

var answer = 42

And later过了一会儿,你需要改变重新赋值成一个字符串:

answer = "Thanks for all the fish..."

JavaScript 解析器会自动将类型进行转换,而不会产生错误。

在表达式中进行数字与字符串的“+”操作时,JavaScript会将数字转换成字符串。例如:

x = "The answer is " + 42 // returns "The answer is 42"
y = 42 + " is the answer" // returns "42 is the answer"

在其他运算符的情况下,JavaScript 不将数字转换成字符串。例如:

"37" - 7 // returns 30
"37" + 7 // returns "377"

{{template.PreviousNext("Core_JavaScript_1.5_Guide:JavaScript_基础", "Core_JavaScript_1.5_Guide:变量")}}

{{ wiki.languages( { "fr": "fr/Guide_JavaScript_1.5/Valeurs", "ja": "ja/Core_JavaScript_1.5_Guide/Values", "ko": "ko/Core_JavaScript_1.5_Guide/Values", "pl": "pl/Przewodnik_po_j\u0119zyku_JavaScript_1.5/Warto\u015bci" } ) }}

修订版来源

<h3 name="Values"> Values </h3>
<p>Javascript识别以下的值类型:
</p>
<ul><li> <a href="cn/Core_JavaScript_1.5_Reference/Global_Objects/Number">Numbers</a>, 如 42 或3.14159
</li><li> <a href="cn/Core_JavaScript_1.5_Reference/Global_Objects/Boolean">Logical (Boolean)</a> values, 可以为<code>true</code> 或者 <code>false</code>
</li><li> <a href="cn/Core_JavaScript_1.5_Reference/Global_Objects/String">Strings</a>, 比如 "Howdy!"
</li><li> <code>null</code>, 一个特殊的空字符; <code>null</code> 也是一个初始值. 因为Javascript区分大小写, <code>null</code> 不同于<code>Null</code>, <code>NULL</code>, 或者其他的变量
</li><li> <code><a href="cn/Core_JavaScript_1.5_Reference/Global_Properties/undefined">undefined</a></code>, 值没有被定义的高级属性; <code>undefined</code> 也是一个初始值.
</li></ul>
<p>This relatively small set of types of values, or <i>data types</i>, enables you to perform useful functions with your applications. There is no explicit distinction between integer and real-valued numbers. Nor is there an explicit date data type in JavaScript. However, you can use the <code><a href="cn/Core_JavaScript_1.5_Reference/Global_Objects/Date">Date</a></code> object and its methods to handle dates.
<a href="cn/Core_JavaScript_1.5_Reference/Global_Objects/Object">Objects</a> and <a href="cn/Core_JavaScript_1.5_Reference/Global_Objects/Function">functions</a> are the other fundamental elements in the language. You can think of objects as named containers for values, and functions as procedures that your application can perform.
</p>
<h4 name=".E7.B1.BB.E5.9E.8B.E8.BD.AC.E6.8D.A2"> 类型转换 </h4>
<p>JavaScript 是一个弱类型的语言。这意味着你在声明变量的时候不需要指定数据类型,程序在执行的过程中数据的类型会根据需要自动的转换。例如:你先声明了下面这个变量: 
</p>
<pre class="eval">var answer = 42
</pre>
<p>And later过了一会儿,你需要改变重新赋值成一个字符串:
</p>
<pre class="eval">answer = "Thanks for all the fish..."
</pre>
<p>JavaScript 解析器会自动将类型进行转换,而不会产生错误。
</p><p>在表达式中进行数字与字符串的“+”操作时,JavaScript会将数字转换成字符串。例如:
</p>
<pre class="eval">x = "The answer is " + 42 // returns "The answer is 42"
y = 42 + " is the answer" // returns "42 is the answer"
</pre>
<p>在其他运算符的情况下,JavaScript 不将数字转换成字符串。例如:
</p>
<pre class="eval">"37" - 7 // returns 30
"37" + 7 // returns "377"
</pre>
<p>{{template.PreviousNext("Core_JavaScript_1.5_Guide:JavaScript_基础", "Core_JavaScript_1.5_Guide:变量")}}
</p>{{ wiki.languages( { "fr": "fr/Guide_JavaScript_1.5/Valeurs", "ja": "ja/Core_JavaScript_1.5_Guide/Values", "ko": "ko/Core_JavaScript_1.5_Guide/Values", "pl": "pl/Przewodnik_po_j\u0119zyku_JavaScript_1.5/Warto\u015bci" } ) }}
恢复到这个版本