数据类型分类
- 基础类型:string,number,boolean,undefined,null,symbol
- 对象类型:object
基础类型为原始值,其值不能被修改;对象类型为引用类型,可以理解为指针,通过引用类型找到内存中的地址,然后再进行相关操作。
// 基本类型var a = 10;var b = a ;a = 20;console.log(b); // 10// 对象类型var c = { name:'hello'};var d = c ;c.name = 'world';console.log(d.name); // 'world'
数据类型判断
- typeof,获取值的所属类型以字符串形式返回
typeof 'hello'; // 'string'typeof 123; // 'number'typeof true; // 'boolean'typeof undefined; // 'undefined'typeof null; // 'object' (特例1)typeof Symbol('hello'); // 'symbol'typeof {a:1} ; // 'object'typeof function(){}; // 'function' (特例2)
特例1,属于基础类型的null
经过typeof
操作符操作后返回的是object
;特例2,函数隶属于对象,typeof
操作直接返回function
。
- instanceof,可以判断通过
new
操作符生成的各种数据类型
var str = 'hello';str instanceof String ; // falsenew String('hello') instanceof String; // true// number,booelan 同理,(null,undefined,symbol没有构造函数)123 instanceof Number; // falsenew Number(123) instanceof Number ; // truetrue instanceof Boolean; // falsenew Boolean(true) instanceof Boolean; // true
instanceof 的实质是,判断 instanceof
左边对象的原型是不是在右边对象的原型链中。
- toString,通过 Object 原型上的 toString 方法,判断对象所属类型
Object.prototype.toString.call('hello'); // "[object String]"Object.prototype.toString.call(123); // "[object Number]"Object.prototype.toString.call(true); // "[object Boolean]"Object.prototype.toString.call(null); // "[object Null]"Object.prototype.toString.call(undefined); // "[object Undefined]"Object.prototype.toString.call(Symbol('hi')); // "[object Symbol]"Object.prototype.toString.call(function(){}); // "[object Function]"Object.prototype.toString.call([]); // "[object Array]"Object.prototype.toString.call(new Date()); // "[object Date]"Object.prototype.toString.call(/w+/); // "[object RegExp]"Object.prototype.toString.call({}); // "[object Object]"
数据的操作方法
基本数据类型的所有方法操作都不改变原始值。对象类型的数据,操作过程中始终留意引用地址的中转概念,其原型上方法有会改变和不改变原值的两种类型。
// 基本类型var str = 'hello';str.toUpperCase(); // 仅返回值为 'HELLO',并不是直接在原值上修改console.log(str); // 'hello'// 对象类型// 改变值var arr1 = [1,2,3];arr1.push(4); arr1; // [1,2,3,4]// 不改变值var arr2 = [5,6,7];arr2.concat(arr1); // 返回 [5,6,7,1,2,3,4]arr2; // [5,6,7]// 引用地址示例var arr3 = [8,9]; // arr3 实则对应的是内存中 [8,9] 数组的引用地址,比如 x0001var arr4 = arr3; // arr4 为 arr3 的值,即为 x0001arr4[0]= 10; // 在对地址进行操作和读值时,则又通过引用找到对应的区域进行读写操作arr3; // [10,9]
引用类型可以简单地类比成计算机中的快捷方式。
常用又较容易混淆的操作方法:
String
- 剪切
方法名 | 说明 |
---|---|
slice(start,end) | start和end都可以为负数,皆代表位置索引,从start往字符串右边向剪切,end位置若在start左边,则返回为空, |
substr(start,length) | start可为负,表示定位到剪切的起始位置,length需为正数,表示从起始位置往右截取的长度。 |
substring(index1,index2) | Index1 和 index2 都为正数皆代表位置索引,两者无序即总是取两者之间的字符 |
Number
- toFixed(length),返回字符串;仅对小数点位后,做特定长度的截取,不够长度则补0,四舍五入。
var num = 123.5368;num.toFixed(2); // '123.54'num.toFixed(1); // '123.5'num.toFixed(6); // '123.536800'
Array
- slice 和 splice
方法名 | 说明 |
---|---|
slice(start,end) | start和end都可以为负数,皆代表位置索引,从start元素往数组右边浅拷贝,不包括end位置元素,end 位置若在 start 左边,则返回空数组(类似字符串的slice),操作不影响原数组。 |
splice(start,len,…eles) | start代表索引位置,可为负;len表示要删除的个数,eles 表示要在删除的位置中插入的元素,可多个。操作会影响到原数组。返回的为删除的元素数组。 |
var arr = [1,2,3,4,5,6,7,8];// slice arr.slice(-4,-1); // [5,6,7]// splicevar data = arr.splice(0,1,99); arr; // [99,2,3,4,5,6,7,8]data; // [1]