首页 >> js开发 >> jstoString.call()通用的判断数据类型方法示例js大全
jstoString.call()通用的判断数据类型方法示例js大全
发布时间: 2021年1月13日 | 浏览:
| 分类:js开发
大家都知道判断数据类型的方法有很多。我们常用的有typeof但是,这个方法有一定的局限性。
typeof null
// "object"
typeof [8]
// "object"
typeof {}
// "object"
typeof function(){}
// "function"
typeof 2
//"number"
typeof ""
//"string"
typeof true
//"boolean"
typeof undefined
//"undefined"
typeof Symbol(2)
// "symbol"
typeof null
// "object"
typeof [8]
// "object"
typeof {}
// "object"
typeof function(){}
// "function"
typeof 2
//"number"
typeof ""
//"string"
typeof true
//"boolean"
typeof undefined
//"undefined"
typeof Symbol(2)
// "symbol"typeof 无法区分null 数组和对象,通常我们会区分判断Array和Object有时会用instanceof 来判断是不是一个对象的实例子
[] instanceof Array
// true 这种方法可以判断数组,不能区分对象
[] instanceof Object
// true
null instanceof Object
// false 也不能区分null
[] instanceof Array
// true 这种方法可以判断数组,不能区分对象
[] instanceof Object
// true
null instanceof Object
// false 也不能区分null
下面介绍一种方法,对每一种数据类型都实用。
toString.call(function(){})
// "[object Function]"
toString.call(null)
//"[object Null]"
toString.call([2])
"[object Array]"
toString.call(undefined)
//"[object Undefined]"
toString.call('stjd')
//"[object String]"
toString.call(1)
//"[object Number]"
toString.call(true)
//"[object Boolean]"
toString.call(Symbol(3))
// "[object Symbol]"
toString.call({q:8})
//"[object Object]"
toString.call(function(){})
// "[object Function]"
toString.call(null)
//"[object Null]"
toString.call([2])
"[object Array]"
toString.call(undefined)
//"[object Undefined]"
toString.call('stjd')
//"[object String]"
toString.call(1)
//"[object Number]"
toString.call(true)
//"[object Boolean]"
toString.call(Symbol(3))
// "[object Symbol]"
toString.call({q:8})
//"[object Object]"
再来思考, toString.call([2]) 意思就是改变方法中的this指向,指向传递进去的参数,也就是[2]。那我这样写不是更直观吗?[2].toString()。结果
[2].toString()
//"2"
[2].toString()
//"2"
var obj = {a: 67}
console.log(obj.toString())
// [object Array]
var obj = {a: 67}
console.log(obj.toString())
// [object Array]
对比上面两个返回的值是不一样的。这是因为[2].toString()调用的是数组的toSting()方法,而不是对象的toSting()方法。Array改写了Object的toString方法。toSting.call()实际上就是 Object.prototype.toSting.call()
console.log(Object.prototype.toString.call([33])) // [object Array]
console.log(Object.prototype.toString.call([33])) // [object Array]
而[2].toSting()实际上是
console.log(Array.prototype.toString.call([2])) //2
console.log(Array.prototype.toString.call([2])) //2
使用的过程中,可以这样封装函数
function isType(type) {
return function(obj) {
return {}.toString.call(obj) == "[object " + type + "]"
}
}
var isObject = isType("Object")
var isString = isType("String")
var isArray = Array.isArray || isType("Array")
var isFunction = isType("Function")
var isUndefined = isType("Undefined")
function isType(type) {
return function(obj) {
return {}.toString.call(obj) == "[object " + type + "]"
}
}
var isObject = isType("Object")
var isString = isType("String")
var isArray = Array.isArray || isType("Array")
var isFunction = isType("Function")
var isUndefined = isType("Undefined")
总结总结
typeof null
// "object"
typeof [8]
// "object"
typeof {}
// "object"
typeof function(){}
// "function"
typeof 2
//"number"
typeof ""
//"string"
typeof true
//"boolean"
typeof undefined
//"undefined"
typeof Symbol(2)
// "symbol"
typeof null
// "object"
typeof [8]
// "object"
typeof {}
// "object"
typeof function(){}
// "function"
typeof 2
//"number"
typeof ""
//"string"
typeof true
//"boolean"
typeof undefined
//"undefined"
typeof Symbol(2)
// "symbol"typeof 无法区分null 数组和对象,通常我们会区分判断Array和Object有时会用instanceof 来判断是不是一个对象的实例子
[] instanceof Array
// true 这种方法可以判断数组,不能区分对象
[] instanceof Object
// true
null instanceof Object
// false 也不能区分null
[] instanceof Array
// true 这种方法可以判断数组,不能区分对象
[] instanceof Object
// true
null instanceof Object
// false 也不能区分null
下面介绍一种方法,对每一种数据类型都实用。
toString.call(function(){})
// "[object Function]"
toString.call(null)
//"[object Null]"
toString.call([2])
"[object Array]"
toString.call(undefined)
//"[object Undefined]"
toString.call('stjd')
//"[object String]"
toString.call(1)
//"[object Number]"
toString.call(true)
//"[object Boolean]"
toString.call(Symbol(3))
// "[object Symbol]"
toString.call({q:8})
//"[object Object]"
toString.call(function(){})
// "[object Function]"
toString.call(null)
//"[object Null]"
toString.call([2])
"[object Array]"
toString.call(undefined)
//"[object Undefined]"
toString.call('stjd')
//"[object String]"
toString.call(1)
//"[object Number]"
toString.call(true)
//"[object Boolean]"
toString.call(Symbol(3))
// "[object Symbol]"
toString.call({q:8})
//"[object Object]"
再来思考, toString.call([2]) 意思就是改变方法中的this指向,指向传递进去的参数,也就是[2]。那我这样写不是更直观吗?[2].toString()。结果
[2].toString()
//"2"
[2].toString()
//"2"
var obj = {a: 67}
console.log(obj.toString())
// [object Array]
var obj = {a: 67}
console.log(obj.toString())
// [object Array]
对比上面两个返回的值是不一样的。这是因为[2].toString()调用的是数组的toSting()方法,而不是对象的toSting()方法。Array改写了Object的toString方法。toSting.call()实际上就是 Object.prototype.toSting.call()
console.log(Object.prototype.toString.call([33])) // [object Array]
console.log(Object.prototype.toString.call([33])) // [object Array]
而[2].toSting()实际上是
console.log(Array.prototype.toString.call([2])) //2
console.log(Array.prototype.toString.call([2])) //2
使用的过程中,可以这样封装函数
function isType(type) {
return function(obj) {
return {}.toString.call(obj) == "[object " + type + "]"
}
}
var isObject = isType("Object")
var isString = isType("String")
var isArray = Array.isArray || isType("Array")
var isFunction = isType("Function")
var isUndefined = isType("Undefined")
function isType(type) {
return function(obj) {
return {}.toString.call(obj) == "[object " + type + "]"
}
}
var isObject = isType("Object")
var isString = isType("String")
var isArray = Array.isArray || isType("Array")
var isFunction = isType("Function")
var isUndefined = isType("Undefined")
总结总结
相关文章:
- JavaScriptNuxt.js nuxt-link与router-link的区别说明
- JavaScriptNodejs + sequelize 实现增删改查操作
- js微信小程序调用后台service教程详解js大全
- js解决VUE项目使用Element-ui 下拉组件的验证失效问题js大全
- js全网小程序接口请求封装实例代码js大全
- jsvue router-link 默认a标签去除下划线的实现js大全
- jsVUE异步更新DOM - 用$nextTick解决DOM视图的问题js大全
- js微信小程序基于ColorUI构建皮皮虾短视频去水印组件js大全
- js解决element-ui的下拉框有值却无法选中的情况js大全
- js解决VueCil代理本地proxytable无效报错404的问题js大全