码迷,mamicode.com
首页 > 编程语言 > 详细

javascript 数据类型

时间:2016-07-06 11:50:01      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:

基本数据类型

Undefined

typeof undefined === "undefined"

null

typeof null === "object"

Boolean

转为false的:

undefined
null
NaN
0
-0
""

转为true的:

[]
{}

Number

溢出:

1/0 === Infinity

-1/0 === -Infinity

特殊:

0/0  // NaN

比较:

NaN === NaN  //false

NaN !== NaN  //true

误差:

0.1 - 0.05 === 0.2 - 0.15  //false

String

运算:
"1" + 2 = "12" "1" * 2 = 2 //区别与ruby等语言 "a" * 2 = NaN 属性:
"abc".length = 3 "abc"[1] = "b"

引用类型

Object

属性访问:
person = {
    "test": "abc",
    "hello world": "haha"
};
person.test === "abc";  //方法1
person[‘hello world‘] === "haha";  //方法2

IN 操作:
‘test‘ in person === true

FOR-IN 操作:
for(var key in person) {
    alert(key)  //一次弹出"test"和"hello world"
}

Array

数组长度:
a = [1,2,3,];
a.length == 3;  //不是4

稀疏数组:
a = [,,,];
0 in a === false;
1 in a === false;

a[10] = 10;
a.length === 10
10 in a === true
1 in a === false

Function

函数声明:

alert(sum(10,10));  //正确
function sum(num1, num2){  //函数声明提前到顶部
    return num1 + num2;
}

alert(sum(10,10));  //报错
var sum = function(num1, num2){  //变量sum的声明提前,但是赋值不提前
    return num1 + num2;
};

包装类型

ECMAScript提供了3个特殊的引用类型:Boolean、Number、String。

每当读取一个基本类型值的时候,后台就会创建一个对应的基本包装类型的对象,从而让我们能够调用一些方法来操作这些数据。

s = "hello world";
s.substring(2);     // "llo world"
s.toUpperCase()     // "HELLO WORLD"

 

javascript 数据类型

标签:

原文地址:http://www.cnblogs.com/licongyu/p/5621701.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!