Manfred Hu的技术博客 http://www.cnblogs.com/manfredHu/ Stay hungry,stay foolish.Keep looking,don't settle.

JS中基本类型与包装类型的关系

对于JS中一些类型的转化的东西,自己测试并得出的结论,有错误的地方请大大们留言。

不多废话,直接贴代码,测试请直接拷贝全部代码:

 

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

<title>Examples</title>

<meta name="description" content="">

<meta name="keywords" content="">

<link href="" rel="stylesheet">

</head>

<body>

    <script type="text/javascript">

    var myNumber = new Number(23);

    var myNumber2 = 23;

    var myString = new String('male');

    var myString2 = 'male';

    var myBoolean = new Boolean(false);

    var myBoolean2 = false;

    var myObject = new Object();

    var myObject2 = {};

    var myArray = new Array('foo','bar');

    var myArray2 = ['foo','bar'];

    var myFunction = new Function('x','y','return x*y');

    var myFunction2 = function(x,y){return x*y};

    var myRegExp = new RegExp('\bt[a-z]+\b');

    var myRegExp2 = /\bt[a-z]+\b/;

    console.log(myNumber.constructor,myNumber2.constructor);

    console.log(myString.constructor,myString2.constructor);

    console.log(myBoolean.constructor,myBoolean2.constructor);

    console.log(myObject.constructor,myObject2.constructor);

    console.log(myArray.constructor,myArray2.constructor);

    console.log(myFunction.constructor,myFunction2.constructor);

    console.log(myRegExp.constructor,myRegExp2.constructor);

    console.log(typeof myNumber);

    console.log(typeof myNumber2);

     </script>

</body>

</html>

 

输出:

 

结论:

JS中的字面量(基本类型)的构造函数为其包装类,也就是说赋值的时候先将基本类型转化为包装类型,然后包装类型再返回一个基本类型的值。而与包装类型对应的为Number,String,Boolean。这是由上面例子得出的。

posted @ 2014-12-09 19:09  manfredHu  阅读(311)  评论(0编辑  收藏  举报
推荐
返回顶部