一、JavaScript数据类型(字面量的类型)
graph LR
JavaScript数据类型--> A(基本数据类型)
A--> 数字Number
数字Number--> 非数字NaN
A--> 字符串String
A--> 布尔值Boolean
A--> 对空Null
A--> 未定义Undefined
A--> Symbol:ES6新增,这种类型的对象永不相等,即使创建的时候传入相同的值
Symbol:ES6新增,这种类型的对象永不相等,即使创建的时候传入相同的值--> 可以解决属性名冲突的问题,做为标记
JavaScript数据类型--> B(引用数据类型)
B--> 对象Object
B--> 数组Array
B--> 函数Function
二、基本数据类型
2.1 强制类型转换(其他类型--String)
方式一:toString()方法
方式二:String()函数
方式三:隐式类型转换(通过 连接符+ 实现)
<script>
/* 将其他数据类型转换为String
方式一:toString()方法
- 原变量的类型不变
- null 和 undefined 没有toString()方法
方式二:String()函数,并将被转换的数据作为参数传递
- 原变量的数据类型为String
- 对于 Number 和 Boolean 实际上就是调用的toString()方法
对于 null 和 undefined 是直接转换的
方式三:通过 连接符+
实际上调用了String()函数
*/
var a = NaN;
a.toString();
console.log(typeof a);
console.log(a);
a = true;
a.toString();
console.log(typeof a);
console.log(a);
a = null;
console.log(typeof a);
// a.toString();
// console.log(typeof a); //不能转换
console.log(a);
a = undefined;
//a.toString(); //不能转换
console.log(typeof a);
console.log(a);
var b = NaN;
b = String(b);
console.log(typeof b);
console.log(b);
b = false;
b = String(b);
console.log(typeof b);
console.log(b);
b = null;
b = String(b);
console.log(typeof b);
console.log(b);
b = undefined;
b = String(b);
console.log(typeof b);
console.log(b);
//隐式转换
var c = 1;
c = c + ""; // 即 c = 1 + ""
console.log(typeof c);
console.log(c);
</script>
2.2 转换为Number
方式一:Number()函数
方式二:parseInt()、parseFloat()
<script>
/* 将其他数据类型转换为String
方式一:toString()方法
- 原变量的类型不变
- null 和 undefined 没有toString()方法
方式二:String()函数,并将被转换的数据作为参数传递
- 原变量的数据类型为String
- 对于 Number 和 Boolean 实际上就是调用的toString()方法
对于 null 和 undefined 是直接转换的
*/
var a = NaN;
a.toString();
console.log(typeof a);
console.log(a);
a = true;
a.toString();
console.log(typeof a);
console.log(a);
a = null;
console.log(typeof a);
// a.toString();
// console.log(typeof a); //不能转换
console.log(a);
a = undefined;
//a.toString(); //不能转换
console.log(typeof a);
console.log(a);
var b = NaN;
b = String(b);
console.log(typeof b);
console.log(b);
b = false;
b = String(b);
console.log(typeof b);
console.log(b);
b = null;
b = String(b);
console.log(typeof b);
console.log(b);
b = undefined;
b = String(b);
console.log(typeof b);
console.log(b);
</script>
2.3 其他进制数
进制数 | js中的表示 | 例子 |
---|---|---|
十六进制 | 0x | 0x123 |
八进制 | 0 | 012 |
二进制 | 0b | 0b12 |
//像"070",有些浏览器会当八进制解析,也有可能当成十进制解析
var a = "070";
//第一个参数是用来指定数字的进制
a = parseInt(a, 10);
console.log(typeof a);
console.log(a);
2.4 转换为Boolean
方式一:Boolean()函数
方式二:隐式类型转换(逻辑运算符 !非)
<script>
/*
其他数据类型转换为Boolean
方式一:使用Boolean()函数
- Number --> Boolean
- 除了0 和NaN,其余都是true
- String --> Boolean
- 除了空串,其余的都是true
- null、undefined --> false
- 对象 --> true
方式二:隐式类型
利用 "!" 非 来进行转换
通过非这个逻辑运算符,非Boolean类型的,会先转换为Boolean类型
单!:转换为Boolean类型的相反值
双!:转换为Boolean类型的原值
*/
var a = 0;
a = Boolean(a);
console.log(typeof a);
console.log("0转换为Boolean:" + a);
a = NaN;
a = Boolean(a);
console.log(typeof a);
console.log("NaN转换为Boolean:" + a);
a = "";
a = Boolean(a);
console.log(typeof a);
console.log("空串转换为Boolean:" + a);
a = null;
a = Boolean(a);
console.log(typeof a);
console.log("null转换为Boolean:" + a);
a = undefined;
a = Boolean(a);
console.log(typeof a);
console.log("undefined转换为Boolean:" + a);
a = new Object();
a = Boolean(a);
console.log(typeof a);
console.log("Object转换为Boolean:" + a);
b = "0";
b = !!b
console.log(typeof b);
console.log("通过逻辑运算符 "!" 转换为Boolean:" + b);
</script>
三、引用数据类型
3.1 数组
var cars = ["Porsche", "Volvo", "BMW"];
3.2 对象
// 方式一:
var person = {
firstName: "Bill",
lastName : "Gates",
id : 678,
fullName : function() {
return this.firstName + " " + this.lastName;
}
};
//方式二:通过 "new" 关键字创建对象
var person = new String(); //定义了一个String对象
访问对象属性的两种方法:
//一般作为静态对象使用时来存取属性
objectName.propertyName
或
//动态存取属性
objectName["propertyName"]
3.3 函数
function name(参数 1, 参数 2, 参数 3) {
要执行的代码
}
四、null和undefined的区别
//清空对象
var person = null; // 值是 null,但是类型仍然是对象
var person = undefined; // 值是 undefined,类型是 undefined
区别:Undefined 与 null 的值相等,但类型不相等
typeof undefined // undefined
typeof null // object
null === undefined // false
null == undefined // true