当前位置: 代码迷 >> Web前端 >> 三 数据类型
  详细解决方案

三 数据类型

热度:956   发布时间:2012-11-07 09:56:10.0
3 数据类型

3.1 Number

8进制数不是标准的,应该避免使用。



??

3.2 String

Number --> String

var abc = 12;
undefined
abc.toString();
"12"
abc.toString(16);
"c"
String(abc);
"12"
abc + "";
"12"
var efg = 123456.789;
undefined
efg.toFixed(0);
"123457"
efg.toFixed(2);
"123456.79"
efg.toExponential(1);
"1.2e+5"
efg.toExponential(3);
"1.235e+5"
efg.toPrecision(4);
"1.235e+5"
efg.toPrecision(7);
"123456.8"

?

String --> Number

var str = "123A";
undefined
Number(str);
NaN
parseInt(str, 16);
4666
var str1 = "123.1";
undefined
str1 - 0;
123.1
Number(str1);
123.1

?

3.3 Boolan

?

Boolean --> Number? true --> 1? false --> 0

Boolean --> String???? true --> "true" false --> "false"

?

Number --> Boolean? NaN, 0 --> false? others--> true

String --> Boolean??? "" --> false others --> true

object --> Boolean?? null --> false others --> true

?

3.4 Function

?

function squre(x) {return x * x;}

?

var squre = function(x) {return x * x;}

?

var plus= Function("x, y", "return x +y;")

?

function squre(x) { return x * x}
undefined
var abc = squre(10);
undefined
abc;
100
var plus = function(x,y){return x + y;}
undefined
var plusRst = plus(10, -9);
undefined
plusRst;
1
var plusRst = Function{"x,y", "return x+y;"};
SyntaxError: Parse error 
var plusRst = Function{"x", "return -x;"};
SyntaxError: Parse error 
var plusRst = Function("x,y", "return x + y;");
undefined
plusRst(10,-8);
2

?

3.5 Object

var point = new Object();
point.x = 10;
point.y = 20;
var rectangle = { upperLeft: { x: 2, y: 2 },
                  lowerRight: { x: 4, y: 4}
                };
var rectangle = { "upperLeft": { x: 2, y: 2 },
                  "lowerRight": { x: 4, y: 4}
                };

?

Object --> String toString()

Object --> Number valueOf()

?????????????????????????????? toString() --> Number

?

3.6 Array

var arr = new Array();
undefined
arr[0] = 1.2;
1.2
arr[1] = "javascript";
"javascript"
arr[2] = true;
true
arr[3] = {x:1, y:3};;
Object
arr;
[1.2, "javascript", true, Object]
var arr2 = new Array(1.2, "javascript", true, {x:1, y:3});
undefined
arr2;
[1.2, "javascript", true, Object]
var arr3 = new Array(10);
undefined
arr3;
[undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined]
arr3.length;
10
var arr4 = [1.2, "javascript", true, {x:1, y:3}];
undefined
arr4;
[1.2, "javascript", true, Object]
var matrix = [[1,2,3], [4,5,6], [7,8,9]];
undefined
matrix;
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
var arr5 = [1,,,,5];
undefined
arr5;
[1, undefined, undefined, undefined, 5]

?

3.7 null

??? null --> 0, "null", false

?

3.8 undefined

null == undefined;
true
null === undefined;
false

?undefined -> NaN, "undefined", false

?

3.9?Date?

var now = new Date();
undefined
"Today is: " + now.toLocaleString();
"Today is: Thursday, April 16, 2009 10:41:58"

?

3.12 Wrapper Object

var otrue = Object(true);
undefined
otrue;
true
typeof(otrue);
"object"

?

3.14 Object To Primitive

做+或者比较操作的时候,Object会先调用valueOf,转换成数字然后比较。如果不能转换成数字,就会转换成String进行比较。

var ofalse = new Boolean(false);
undefined
if (ofalse) a = 10; else a =11;
10
var arr1 = [];
undefined
arr - 0;
NaN
arr1 - 0;
0
var arr2 = [1];
undefined
arr2 - 0;
1
var arr3 = [1,2];
undefined
arr3 - 0;
NaN
var str = "3";
undefined
arr2 < str;
true
"2" < "3";
true
"2" < "3";
true
"A" < "a";
true
var str2 = "1";
undefined
arr2 < str2;
false

?

3.15?Reference or Value

?

数字,bool类型都是value的,object都是reference的。string是不可改变的,传得是reference, == 比较的是value。

  相关解决方案