1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- <script>
-
- /*
- 基本数据类型
- String Number Boolean Null Undefined
- 引用数据类型
- Object
- 在JS中为我们提供了三个包装类,通过这三个包装类,可以将基本数据类型的数据转换为对象
- String()
- -可以将基本数据类型字符串转换为String对象
- Number()
- -可以将基本数据类型的数字转换为Number对象
- Boolean()
- -可以将基本数据类型的布尔值转换为Boolean对象
- 但是注意:我们在实际应用中不会使用基本数据类型的对象,
- 如果使用基本数据类型的对象,在做一些比较时可能会带来一些不可预期的结果
- */
- //创建一个Number类型的对象
- var num = new Number(3);
- // console.log(num);
- // console.log(typeof num);
- var str =new String("hello");
- // console.log(str);
- // console.log(typeof str);
- var bool = new Boolean(true);
- // console.log(bool);
- // console.log(typeof bool);
- //向num中添加一个属性
- // var num = new Number(3);
- // num.hello = "abc";
- // console.log(num.hello);//输出结果为abc
- // var a = 3;
- // a.hello="abc";
- // console.log(a.hello);//输出结果为undefined
- // var num = new Number(3);
- // var num2 = new Number(3);
- // console.log(num == num2);//false
- var bool = new Boolean(true);
- var bool2 = true;
- // console.log(bool == bool2);//true
- // console.log(bool === bool2);//false
- var b = new Boolean(false);
- // if(b){
- // alert("我运行了~~");
- // }
- /*
- 方法和属性只能添加给对象,不能添加给基本数据类型
- 当我们对一些基本数据类型的值去调用属性和方法时,
- 浏览器会临时使用包装类将其转换为对象,然后再调用对象的属性和方法
- 调用完以后,再将其转换为基本数据类型
- */
- // var s = 123;
- // s = s.toString();
- // console.log(s);
- // console.log(typeof s);
- var s = 123;
- s = s.toString();
- s.hello = "你好";
- console.log(s.hello);
-
- </script>
- </head>
- <body>
-
- </body>
- </html>
|