数据类型转换中的一些特殊情况(JY06-JavaScript)
1.字符串的不可变性
字符串定义了后,会一直占据内存空间,企鹅该处内存空间(栈)不可被重新赋值。
2.短路运算
||、&& 二元运算符,返回参与运算的操作数的原值(原数据类型和原数据),
运算结束后,返回导致运算结束的那个操作数。
3.三元运算符
code1?code2:code3; 与if-else 不同:
返回code2或code3的值----code2,code3 都可以空的{}代替;
不能写break,continue。
4.NaN
NaN !=NaN,
任何NaN参与的数学运算,其结果都是NaN
有NaN参与的条件表达式: 比较运算符 >/>=/</<=/==/=== 运算结果为false
!==/!= 运算结果为true
- <script>
- var a;
- console.log(Boolean(NaN>=4));
- console.log(Boolean(NaN<4));
- console.log(Boolean(NaN=4));
- console.log(Boolean(NaN==4));
- console.log(Boolean(a=4));
- console.log(NaN);
- console.log(a);
- if(NaN==NaN){
- a = "NaN==NaN";
- }
- var b;
- if(NaN!==NaN){
- b = "NaN!=NaN";
- }
- console.log(a+"\n"+b);
- </script>
---NaN特点演示
5. JS 简单数据类型的转换---特殊情况演示
数据:0,“”,false,null,undefined,"123abc"等
- <!DOCTYPE html>
- <html>
- <head lang="en">
- <meta charset="UTF-8">
- <title></title>
- <style>
- div {
- line-height: 24px;
- margin: 0;
- padding: 0;
- }
- .one {
- width: 920px;
- position: absolute;
- left: 50%;
- top: 50%;
- margin-left: -460px;
- margin-top: -240px;
- }
- .all {
- float: left;
- border: 2px solid #000000;
- }
- .all-top {
- font-size: 20px;
- font-weight: bold;
- }
- .all-bottom {
- line-height: 48px;
- font-size: 16px;
- }
- .details {
- float: left;
- border: 2px solid #000000;
- line-height: 24px;
- margin-left: -2px;
- }
- .details:hover {
- position: relative;
- border: 2px solid #ff0000;
- }
- .line-long {
- border-top: 2px solid #000000;
- height: 0;
- width: 908px;
- }
- .line-short {
- border-top: 2px dashed #000000;
- height: 0;
- width: 742px;
- margin-left: 166px;
- }
- </style>
- <script>
- document.write("<div class='one'>");
- function f1() {
- return typeof res[res.length - 1];
- }
- var arr = [0, "", false, null, undefined, NaN, 6.66, -9, "abc124", "-12.23abc23", "qwer", "s s"];
- document.write("<div class='all'><div class='all-top'>" + " 原数据及类型" + "</br>" + "转换方法 </div>" + "<div class='all-bottom'>" + "+" + "</br>" + "Number()" + "</br>" + "parseInt()" + "</br>" + "parseFloat()" + "</br>" + "\"\"" + "</br>" + ".toString" + "</br>" + "String()" + "</br>" + "!!" + "</br>" + "Boolean()" + "</br>" + "</div></div>")
- for (var i = 0; i < arr.length; i++) {
- switch (true) {
- case arr[i] === "":
- {
- var res = ['""'];
- break;
- }
- default :
- {
- var res = [arr[i] + ""];
- }
- }
- res[res.length] = typeof arr[i];
- res[res.length] = +arr[i];
- res[res.length] = f1();
- res[res.length] = Number(arr[i]);
- res[res.length] = f1();
- res[res.length] = parseInt(arr[i]);
- res[res.length] = f1();
- res[res.length] = parseFloat(arr[i]);
- res[res.length] = f1();
- res[res.length] = arr[i] + "";
- res[res.length] = f1();
- if (i == 3 || i == 4) {//null 和undefined没有.toString()方法,导致报错
- res[res.length] = "报错";
- res[res.length] = "报错";
- } else {
- res[res.length] = (arr[i]).toString();
- res[res.length] = f1();
- }
- res[res.length] = String(arr[i]);
- res[res.length] = f1();
- res[res.length] = !!arr[i];
- res[res.length] = f1();
- res[res.length] = Boolean(arr[i]);
- res[res.length] = f1();
- var resString = res.join("<br>");
- document.write("<div class='details'>" + resString + "</br>" + "</div>");
- }
- var j = 22;
- for (var i = 0; i < 9; i++) {
- document.write("<div class='line-short' style='margin-top:" + j + "px'></div>")
- document.write("<div class='line-long' style='margin-top:" + j + "px'></div>")
- }
- document.write("</div>");
- </script>
- </head>
- <body>
- </body>
- </html>
数据类型转换中的一些特殊情况(JY06-JavaScript)的更多相关文章
- Dynamics CRM 通过OData查询数据URI中包含中文的情况
filter条件如下"?$filter=new_name eq '采购主管' and new_entityname eq 'new_purchaseenquiry' ",如果用这个 ...
- [.net 面向对象编程基础] (4) 基础中的基础——数据类型转换
[.net面向对象编程基础] (4)基础中的基础——数据类型转换 1.为什么要进行数据转换? 首先,为什么要进行数据转换,拿值类型例子说明一下, 比如:我们要把23角零钱,换成2.30元,就需要把整形 ...
- SQL中数据类型转换
CAST 和 CONVERT 将某种数据类型的表达式显式转换为另一种数据类型.CAST 和 CONVERT 提供相似的功能. 语法 使用 CAST: CAST ( expression AS data ...
- matlab中图片数据类型转换uint8与double
matlab中处理图像像素点数据: img1=double(imread('lenna.bmp')); matlab中imshow图片,要先转换成uint8: subplot(1,2,1),imsho ...
- Java中数据类型转换&基本类型变量和对象型变量
1.Java的数据类型分为三大类 布尔型,字符型和数值型 其中数值型又分为整型和浮点型 2.Java的变量类型 布尔型 boolean 字符型 char 整型 byte,short,int,lo ...
- JS中数据类型转换
JS中数据类型转换汇总 JS中的数据类型分为 [基本数据类型] 数字 number 字符串 string 布尔 boolean 空 null 未定义 undefined [引用数据类型] 对象 obj ...
- JavaScript中数据类型转换总结
JavaScript中数据类型转换总结 在js中,数据类型转换分为显式数据类型转换和隐式数据类型转换. 1, 显式数据类型转换 a:转数字: 1)Number转换: 代码: var a = " ...
- C语言中强制数据类型转换(转)
原文地址不详 字符型变量的值实质上是一个8位的整数值,因此取值范围一般是-128-127,char型变量也可以加修饰符unsigned,则unsigned char 型变量的取值范围是0-255(有些 ...
- Java基础知识强化22:Java中数据类型转换
数据类型转换: (1). 自动转换 低级变量可以直接转换为高级变量,这叫自动类型转换.比如: byte b: int b: long b: float b: double b: 上面的语句可 ...
随机推荐
- Java简介(3)-基本语法
1.大小写敏感 2.类名 3.方法名. 4.源文件名
- python学习第十七天 --定制类
何为定制类? Python的class允许定义许多特殊方法,可以让我们非常方便地生成特定的类.在类中应用或者重写python的特殊方法,得到的类,就是定制类. 大家都知道print的用法.见下面例子 ...
- POJ - 3903 Stock Exchange(LIS最长上升子序列问题)
E - LIS Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Descripti ...
- UVa 10020 - Minimal coverage(区间覆盖并贪心)
Given several segments of line (int the X axis) with coordinates [Li, Ri]. You are to choose the min ...
- [转]Windows平台下安装Hadoop
1.安装JDK1.6或更高版本 官网下载JDK,安装时注意,最好不要安装到带有空格的路径名下,例如:Programe Files,否则在配置Hadoop的配置文件时会找不到JDK(按相关说法,配置文件 ...
- angular2 学习笔记 ( rxjs 流 )
RxJS 博大精深,看了好几篇文章都没有明白. 范围牵扯到了函数响应式开发去了... 我对函数式一知半解, 响应式更是第一次听到... 唉...不过日子还是得过...混着过先呗 我目前所理解的很浅, ...
- Keil C51库函数原型列表
//1. CTYPE.H bit isalnum(char c): bit isalpha(char c): bit iscntrl(char c): bit isdigit(char c): bit ...
- Linux&shell之处理用户输入
写在前面:案例.常用.归类.解释说明.(By Jim) 命令行参数$1为第一个参数,$2为第二个参数,依次类推...示例: #!/bin/bash # using one command line p ...
- 最小费用最大流MCMF 最小增广
没有写单纯性的...应该不会有卡最小增广的出题人吧...(雾) struct MCMF{ struct tedge{int x,y,cap,flow,w,next;}adj[maxm];int ms, ...
- Hadoop的基本命令【转载】
在这篇文章中,我们默认认为Hadoop环境已经由运维人员配置好直接可以使用. 假设Hadoop的安装目录HADOOP_HOME为/home/admin/hadoop. 启动与关闭 启动HADOOP 进 ...