当前位置:网站首页>Four methods for judging JS data types and user-defined methods
Four methods for judging JS data types and user-defined methods
2022-06-13 04:43:00 【Dax1_】
Preface
stay ECMAScript Specification , Altogether 7 Data type in , It is divided into basic types and reference types ( Complex type ),
- Basic types :String,Number,Boolean,Symbol,Null,Undefined
- Reference type :Object
Method 1 ,typeof()
Use typeof Directly return the data type field , But it's impossible to judge Null, Array , object
console.log(typeof bool); //boolean
console.log(typeof num);//number
console.log(typeof str);//string
console.log(typeof und);//undefined
console.log(typeof nul);//object
console.log(typeof arr);//object
console.log(typeof obj);//object
console.log(typeof fun);//function
It can be seen that , about Null, Array , object ,typeof The test results are object.
For function types , return Function
Method 2 ,instance of()
instance of Used to determine A Is it B Example , It is impossible to determine which type an object instance belongs to .
instance of What we tested was Prototype
console.log(bool instanceof Boolean);// false
console.log(num instanceof Number);// false
console.log(str instanceof String);// false
console.log(und instanceof Object);// false
console.log(arr instanceof Array);// true
console.log(nul instanceof Object);// false
console.log(obj instanceof Object);// true
console.log(fun instanceof Function);// true
var bool2 = new Boolean()
console.log(bool2 instanceof Boolean);// true
var num2 = new Number()
console.log(num2 instanceof Number);// true
var str2 = new String()
console.log(str2 instanceof String);// true
function Person(){
}
var per = new Person()
console.log(per instanceof Person);// true
function Student(){
}
Student.prototype = new Person()
var haoxl = new Student()
console.log(haoxl instanceof Student);// true
console.log(haoxl instanceof Person);// true
instance of There is no difference Null and Undefined
Method 3 ,constructor
undefined and null No, contructor attribute
console.log(bool.constructor === Boolean);// true
console.log(num.constructor === Number);// true
console.log(str.constructor === String);// true
console.log(arr.constructor === Array);// true
console.log(obj.constructor === Object);// true
console.log(fun.constructor === Function);// true
console.log(haoxl.constructor === Student);// false
console.log(haoxl.constructor === Person);// true
Method four ,toString()
toString() yes Object Prototype method of , Call the method , Returns the current object's [[Class]] . This is an internal property , The format for [object Xxx] , among Xxx Is the type of object .
about Object object , Call directly toString() Can return [object Object] . For other objects , You have to go through call / apply To return the correct type information .
call() The method can change this The direction of , Then put the Object.prototype.toString() Methods point to different data types , Return different results
Object.prototype.toString.call(1)
"[object Number]"
Object.prototype.toString.call(NaN);
"[object Number]"
Object.prototype.toString.call("1");
"[object String]"
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(function a() {
});
"[object Function]"
Object.prototype.toString.call([]);
"[object Array]"
Object.prototype.toString.call({
});
"[object Object]"
Customize a perfect method to judge the data type
Reference documents : javascript Several ways to determine the type of data
function _typeof(obj){
var s = Object.prototype.toString.call(obj);
return s.match(/\[object (.*?)\]/)[1].toLowerCase();
};
_typeof([12,3,343]);
"array"
_typeof({
name: 'zxc', age: 18});
"object"
_typeof(1);
"number"
_typeof("1");
"string"
_typeof(null);
"null"
_typeof(undefined);
"undefined"
_typeof(NaN);
"number"
_typeof(Date);
"function"
_typeof(new Date());
"date"
_typeof(new RegExp());
"regexp"
边栏推荐
猜你喜欢
Express scaffold creation
CTFSHOW SQL注入篇(231-253)
Crawler scrapy framework learning 1
PowerShell plus domain add computer module
Createanonymousthreadx passes parameters to anonymous threads
2022 ICML | Pocket2Mol: Efficient Molecular Sampling Based on 3D Protein Pockets
Analysis of scoped attribute principle and depth action selector
Explain the differences and usage scenarios between created and mounted
第007天:go语言字符串
[try to hack] upload labs (temporarily write to 12)
随机推荐
Your one-on-one meetings are inefficient. You can do this!
NodeJS 解析 GET 请求 url 字符串
Trust programming - linked lists: use struct to implement linked lists, use heap to merge K ascending linked lists, and customize display
Express scaffold creation
是“凯撒密码”呀。(*‘▽‘*)*
Read paper 20 together: spatiotemporal prediction of PM2.5 concentration by idw-blstm under different time granularity
Third party comment plugin
PHP security development 15 user password modification module
约瑟夫问题
Li Kou brush question 338 Bit count
Flutter dart variables and constants
Collection of wrong questions in soft test -- morning questions in the first half of 2010
记录一次排查问题的经过——视频通话无法接起
Collection of some compatibility issues
推荐的图片临时在线压缩工具
Applet waterfall flow
JS, how to add grid style
Crawler scrapy framework learning 2
How to understand JS expressions and JS statements
如何优雅的处理async/await错误信息