Data Type Conversion
1. Array length
Truncating an array by setting its length property is the only way that you can actually shorten an array. If you use the delete operator to delete an array element, that element becomes undefined, but the length property does not change.
2. Array.concat( )
If any of these arguments is itself an array, it is flattened and its elements are added to the returned array. Note, however, that concat( ) does not recursively flatten arrays of arrays. Here are some examples:
var a = [1,2,3];
a.concat([4,5],[6,7]) // Returns [1,2,3,4,5,6,7]
a.concat(4, [5,[6,7]]) // Returns [1,2,3,4,5,[6,7]]
//indicate that concat( ) does not recursively flatten arrays of arrays
3. Object-to-Primitive Conversion
a. object->boolean:
whenever a non-null object is used in a boolean context, it converts to true. This is true for all objects (including all arrays and functions), even wrapper objects that represent primitive values that convert to false. For example:
a[0]=new Boolean(false) // Internal value is false, but object converts to true
a[1]=new Number(0)
a[2]=new String("")
a[3]=new Array( )
if(a[0]&&a[1]&&a[2]&&a[3]){
document.write("all are true");
}
//output: all are true.
b. object->number:
objects are converted to numbers by first calling the valueOf( ) method of the object. Most objects inherit the default valueOf( ) method of Object, which simply returns the object itself. Since the default valueOf( ) method does not return a primitive value, JavaScript next tries to convert the object to a number by calling its toString( ) method and converting the resulting string to a number.
Array->number:
Call the toString( ) method of arrays converts the array elements to strings, then returns the result of concatenating these strings, with commas in between. Therefore, an array with no elements converts to the empty string, which converts to the number zero! Also, if an array has a single element that is a number n, the array converts to a string representation of n, which is then converted back to n itself. If an array contains more than one element, or if its one element is not a number, the array converts to NaN.For example:
b=new Array();
b[0]="1";
document.write(10-b); //output:9
document.write(10+b); //output:101
b[0]="w";
document.write(10-b); //output:NaN
document.write(10+b); //output:10w
b[1]=3;
document.write(10-b); //output:NaN
document.write(10+b); //output:10w,3
Strings ->Numbers
parseInt( ) and parseFloat( ) functions convert and return any number at the beginning of a string, ignoring any trailing non-numbers. (P.S. This point is different from Java which will throw java.lang.NumberFormatException).
parseInt( ) can even take a second argument specifying the radix (base) of the number to be parsed. Legal values are between 2 and 36. For example:
var a="12,3";
document.write(a-1); // output: NaN
document.write(parseInt(a)); // output:12
document.write(parseInt(a-1)); // output: NaN
document.write(parseInt("11", 2)); // output: 3 (1*2 + 1)
分享到:
相关推荐
If you want to learn some basic programming concepts, have experience but need help updating your skills, or you’re coming from another language, Beginning JavaScript with DOM Scripting and Ajax can...
If you want to learn some basic programming concepts, have experience but need help updating your skills, or you’re coming from another language, Beginning JavaScript with DOM Scripting and Ajax can...
PATCH-更新系统是坚如磐石,专业,智能,清洁的解决方案,用于管理和分发游戏和应用程序的更新。 您的玩家最终可以毫无痛苦地更新您的游戏,而不必在新版本到来时再次下载整个游戏。这将为他们节省大量带宽和时间!...
- **Updating variablessuccinctly**:简洁地更新变量的方法。 - **Dispatching on a value with switch**:使用 `switch` 语句根据不同的值来执行不同的代码段。 #### 4. **Capitalization** 本节介绍了一些编码...
4. 在右侧的设置面板中找到`Download repository index updates on startup`选项。 5. 将该选项前的勾选取消,点击`Apply`应用更改,然后点击`OK`完成设置。 完成以上步骤后,MyEclipse将不再于启动时自动更新索引...
### IntelliJ IDEA 文件持续闪烁与Updating and Indexing问题详解 #### 一、问题概述 在使用IntelliJ IDEA进行开发过程中,部分用户可能会遇到一个较为常见的现象:IDE中的文件图标会不断闪烁,同时状态栏提示...
针对“PATCH-Updating System.zip”这个压缩包文件,我们可以推断它包含了一个与Unity引擎相关的版本更新或补丁包,主要服务于PC平台。下面将详细讨论Unity引擎的版本更新、补丁管理和Unitypackage文件格式。 Unity...
3. 控制流:包括条件执行(Conditionalexecution)、while和do循环(while and do loops)、for循环(for loops)、跳出循环(Breaking Out of a Loop)、更新变量(Updating variables succinctly)等控制结构的...
在神经网络中更新权重是深度学习模型训练过程中的核心步骤,这一过程直接影响着模型的性能和学习能力。本文将深入探讨“更新神经网络权重”的概念、相关算法以及其在实际应用中的重要性。 首先,神经网络的权重表示...
ATCH-更新系统是坚如磐石,专业,智能,清洁的解决方案,用于管理和分发游戏和应用程序的更新。 您的玩家最终可以轻松无忧地更新游戏,而无需在新版本到来时再次下载整个游戏。这将为他们节省大量带宽和时间!...
PATCH - Updating System 是一款耐用、专业、智能而整洁的解决方案,用于为游戏和应用程序管理及分发更新。 你的玩家终于可以无后顾之忧地更新游戏了,再也不必在新版本发布时重新下载整个游戏。这可以为他们节省...
Unity的更新系统!
【ape-updating】是一款专为前端开发者设计的开源库,主要功能是协助管理和更新项目中的依赖库。在快速发展的前端领域,保持项目依赖的最新状态至关重要,因为这不仅能确保利用最新的特性和性能优化,还能及时修复...
React Native 中的组件生命周期包括挂载(mounting)、更新(updating)和卸载(unmounting)等阶段。 7. 样式与布局: 样式和布局在 React Native 开发中至关重要。开发者可以使用 JSX 直接编写样式代码,React ...
Chapter 4: Updating Documents 4.1Update operators 4.2Array Update Operators 4.3Transactions Chapter 5: Managing Availability and Performance 5.1Indexing 5.2Availability through Replication 5.3 ...
### 模型更新在阻尼结构中的应用及发展 #### 引言 在工程实践中,准确的数学模型对于表示各种工程结构的动力特性至关重要。然而,当前的有限元分析(FEA)方法并不能提供足够精确的有限元(FE)模型,这些模型往往...
`Updating Form from Another Thread without Creating Delegates for Every Type of Update`这个主题探讨的是如何在不为每种类型的更新创建单独委托的情况下,从其他线程安全地更新UI。 通常,当你尝试在非UI线程...
Dynamic updating of the rough approximations is a critical factor for the success of the rough set theory since data is growing at an unprecedented rate in the information-explosion era. Though many ...
cmpr 软件的更新软件,一般浏览器网页下载不下来。 cmpr_updating