We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
javaScript的类型 包含了 ‘基本数据类型’ 和 ‘引用数据类型’
let Car = function() {} let benz = new Car() benz instanceof Car // true
console.log(Object.prototype.toString.call(undefined)) // [object Undefined] console.log(Object.prototype.toString.call(null)) // [object Null] var date = new Date(); console.log(Object.prototype.toString.call(date)) // [object Date] var number = 1; // [object Number] var string = '123'; // [object String] var boolean = true; // [object Boolean] var und = undefined; // [object Undefined] var nul = null; // [object Null] var obj = {a: 1} // [object Object] var symbol = symbol(1) // [object Symbol ] var array = [1, 2, 3]; // [object Array] var date = new Date(); // [object Date] var error = new Error(); // [object Error] var reg = /a/g; // [object RegExp] var func = function a(){}; // [object Function] console.log(Object.prototype.toString.call(Math)); // [object Math] console.log(Object.prototype.toString.call(JSON)); // [object JSON] function a() { console.log(Object.prototype.toString.call(arguments)); // [object Arguments] } a();
function myInstanceof(left, right) { // 这里先用typeof来判断基础数据类型,如果是,直接返回false if(typeof left !== 'object' || left === null) return false; // getProtypeOf是Object对象自带的API,能够拿到参数的原型对象 let proto = Object.getPrototypeOf(left); while(true) { //循环往下寻找,直到找到相同的原型对象 if(proto === null) return false; if(proto === right.prototype) return true;//找到相同原型对象,返回true proto = Object.getPrototypeof(proto); } } // 验证一下自己实现的myInstanceof是否OK console.log(myInstanceof(new Number(123), Number)); // true
function isEmptyObject( obj ) { var name; for ( name in obj ) { return false; } return true; }
在 JavaScript 中,语言所能够保证“安全”(精确)地进行计算的最大整数是 2^53 - 1 ,也就是 9007199254740991 (常量 Number.MAX_SAFE_INTEGER)超过了这个数值,JS 就会产生一些意想不到的问题。比如:
9007199254740991 + 1 // => 结果是 9007199254740992 9007199254740991 + 2 // => 结果也是 9007199254740992
BigInt的提出,解决超过最大整数计算问题, n: 表示超过最大精度的运算
9007199254740991n + 1n // => 结果是 9007199254740992n 9007199254740991n + 4n // => 结果是 9007199254740995n
function getType(obj) { const lowerCaseTheFirstLetter = (str) => str[0].toLowerCase() + str.slice(1); const type = typeof obj; if (type !== 'object') { return type; } return lowerCaseTheFirstLetter( Object.prototype.toString.call(obj).replace(/^\[object (\S+)\]$/, '$1') ); } getType([]); // "array" getType('123'); // "string" getType(null); // "null" getType(undefined); // "undefined" getType(); // "undefined" getType(function () {}); // "function" getType(/123/g); // "regExp" getType(new Date()); // "date" getType(new Map()); // "map" getType(new Set()); // "set"
The text was updated successfully, but these errors were encountered:
No branches or pull requests
javaScript的类型 包含了 ‘基本数据类型’ 和 ‘引用数据类型’
基本数据类型
引用数据类型 (Object)
基本数据类型 和 引用 数据类型的存储方式
如何判断 数据类型
原理:
当 toString 方法被调用的时候,下面的步骤会被执行:
如何实现一个 instanceof
判断对象是否为空 EmptyObject
关于BigInt 的出现解释
在 JavaScript 中,语言所能够保证“安全”(精确)地进行计算的最大整数是 2^53 - 1 ,也就是 9007199254740991 (常量 Number.MAX_SAFE_INTEGER)超过了这个数值,JS 就会产生一些意想不到的问题。比如:
BigInt的提出,解决超过最大整数计算问题, n: 表示超过最大精度的运算
实现一个类型校验工具
The text was updated successfully, but these errors were encountered: