广州开发网站建设,html5手机网页模板,网站设计模式有哪些,制作简单门户网站步骤一、数据类型
1. JavaScript用哪些数据类型、它们有什么区别#xff1f;
JavaScript共有八种数据类型#xff0c;分别包括5种基本数据类型和3种非基本数据类型。
基本数据类型#xff1a;Undefined、Null、Boolean、Number、String。非基本数据类型#xff1a;Object、S…一、数据类型
1. JavaScript用哪些数据类型、它们有什么区别
JavaScript共有八种数据类型分别包括5种基本数据类型和3种非基本数据类型。
基本数据类型Undefined、Null、Boolean、Number、String。非基本数据类型Object、Symbol、BigInt。
其中Symbol和BigInt是ES6新增的数据类型
Symbol代表创建后独一无二且不可变的数据类型它主要是为了解决可能出现的全局变量冲突的问题。BigInt是一种数字类型的数据它可以表示任意精度格式的整数使用BigInt可以安全地存储和大整数即使这个数超出了Number的安全整数范围。
区别一分为原始数据类型和引用数据类型。
原始数据类型Undefined、Null、Boolean、Number、String。引用数据类型Object。另外还有数组Array和函数Function。
区别二存储位置不同。
原始数据类型直接存储在**栈stack**中往往占据空间小、大小固定、属于被频繁使用数据所以放在栈中。引用数据类型存储在**堆heap**中往往占据空间大、大小不固定如果存在栈中将会影响程序运行的性能。因此引用数据类型在栈中存储了指针指针指向堆中该实体的起始地址。当解释器寻找引用值时会先检索其在栈中的地址再根据地址从堆中获得实体。
扩展知识堆与栈
堆和栈的概念存在于数据结构和操作系统内存中。
在数据结构中 栈先进后出堆先进先出 在操作系统中分为堆区和栈区 栈区内存由编译器自动分配释放存放函数的参数值局部变量的值等。操作方式类似于数据结构中的栈。堆区内存一般由开发者分配释放若开发者不释放程序结束时可能由垃圾回收机制回收。
2. 数据类型的检测方式有哪些详细讲讲
2.1 typeof
// 1.typeof 数组、对象和null都会被视为object 其他类型都判定正确
console.log(typeof {}); // object
console.log(typeof []); // object
console.log(typeof null); // object
console.log(typeof function () {}); // function
console.log(typeof 1); // number
console.log(typeof true); // boolean
console.log(typeof str); // string
console.log(typeof undefined); // undefined
console.log(typeof Symbol()); // symbol根据上面的结果可以看到数组、对象和null都会被视为object其他类型都能判定正确。
2.2 instanceof
它的原理是判断在其原型链中能否找到该类型的原型
console.log(2 instanceof Number); // false
console.log(true instanceof Boolean); // false
console.log(str instanceof String); // falseconsole.log([] instanceof Array); // true
console.log(function () {} instanceof Function); // true
console.log({} instanceof Object); // true
console.log(null instanceof Object); // false 这是因为null是原型链的尽头 它没有后续原型链 更不可能找到Object类型的原型根据上面的结果可以看到 instanceof只能正确判断引用类型基本数据类型无法判定。
需要注意的是null instanceof Object结果是false因为null是原型链的尽头它没有后续原型链更不可能找到Object类型的原型。
2.3 constructor
它的原理是**除了null之外任何对象都会在其prototype/__proto__上有一个constructor属性而constructor属性返回一个引用这个引用指向创建该对象的构造函数而Number、Boolean、String、Array都属于构造函数。**因此通过construct和构造函数就能判断类型是否符合。
console.log((2).constructor); // ƒ Number() { [native code] } Number构造函数
console.log((2).constructor Number); // true
console.log((true).constructor Boolean); // true
console.log((str).constructor String); // true
console.log(([]).constructor Array); // true
console.log((function () {}).constructor Function); // true
console.log(({}).constructor Object); // true
// console.log((null).constructor Object); // 会报错 原因是null不存在constructor
// console.log((undefined).constructor Object); // 会报错 原因是undefined不存在constructor
// console.log((null).constructor); // 会报错
// console.log((undefined).constructor); // 会报错从上面的结果看到constructor除了不能判断null和undefined外其它类型都能判断。
需要注意的是如果创建的对象的原型被改变了constructor就不能用来判断数据类型了。
function Fn() {}
console.log(Fn.prototype.constructor); // f Fn() {}
Fn.prototype new Array()
console.log(Fn.prototype.constructor); // ƒ Array() { [native code] }
let f new Fn();
console.log(f.constructor); // ƒ Array() { [native code] }
console.log(f.__proto__); // ƒ Array() { [native code] }
console.log(f.constructor Fn); // false
console.log(f.constructor Array); // true扩展知识constructor的两个作用
判断数据类型。对象实例通过construct对象访问它的构造函数。
2.4 Object.prototype.toString.call()
它的原理是:对象原型上的toString方法会获取当前对象的类型然后返回[object Type]字符串由于部分内置对象对toString重写了因此需要调用.call()来利用原本的toString函数.call(args)方法实现让调用call方法的对象的this指向传的参数args。
let a Object.prototype.toString;
console.log(a.call(2)); // [object Number]
console.log(a.call(2) Number); // false
console.log(a.call(2) [object Number]); // trueconsole.log(a.call(true)); // [object Boolean]
console.log(a.call(true) Boolean); // false
console.log(a.call(true) [object Boolean]); // trueconsole.log(a.call(str)); // [object String]
console.log(a.call(str) String); // false
console.log(a.call(str) [object String]); // trueconsole.log(a.call(new Date())); // [object Date]
console.log(a.call(new Date()) Date); // false
console.log(a.call(new Date()) [object Date]); // trueconsole.log(a.call([])); // [object Array]
console.log(a.call(function () {})); // [object function]
console.log(a.call({})); // [object Object]
console.log(a.call(undefined)); // [object undefined]
console.log(a.call(null)); // [object Null]通过上面代码可以看到Object.prototype.toString.call()可以验证任何类型。
2.5 封装一个类型验证的方法
大型项目中往往会使用Object.prototype.toString.call()封装一个isType方法来验证类型封装代码如下
function isType(data, type) {const typeObj {[object String]: string,[object Number]: number,[object Boolean]: boolean,[object Null]: null,[object Undefined]: undefined,[object Object]: object,[object Array]: array,[object Function]: function,[object Date]: date, // Object.prototype.toString.call(new Date())[object RegExp]: regExp,[object Map]: map,[object Set]: set,[object HTMLDivElement]: dom, // document.querySelector(#app)[object WeakMap]: weakMap,[object Window]: window, // Object.prototype.toString.call(window)[object Error]: error, // new Error(1)[object Arguments]: arguments,};let name Object.prototype.toString.call(data); // 借用Object.prototype.toString()获取数据类型let typeName typeObj[name] || 未知类型; // 匹配数据类型return typeName type; // 判断该数据类型是否为传入的类型
}下面我们可以测试一下封装结果
console.log(isType({}, object), // trueisType([], array), // trueisType(new Date(), object), // falseisType(new Date(), date) // true
);2.6 总结
方法名效果typeof数组、对象和null都会被视为object其他类型都能判定正确instanceof只能正确判断引用类型基本数据类型无法判定constructor除了不能判断null和undefined外其它类型都能判断Object.prototype.toString.call()可以判断所有类型但是返回结果是字符串【最推荐封装isType】
3. 判断数组的方式有哪些
let arr []Object.prototype.toString.call(arr).slice(8,-1) Array 或者Object.protoType.toString.call(arr) [object Array]通过原型链判断:arr.__proto__ Array.prototype通过Array.isArray(arr)通过arr instanceof Array
4. null和undefined有什么区别
含义不同undefined代表的含义是未定义而null代表的含义是空对象。初始化场景不同通常变量声明了但还没有定义的时候使用undefinednull主要用在初始化一些可能会返回对象的变量。typeof判断结果不同typeof undefined返回undefinedtypeof null返回object
一般变量声明了但还没定义的时候会返回undefined。
需要注意的是
使用null undefined 返回true,null undefined返回false。