如何利用js语法判断数据类型

359 ℃

typeof方法

typeof 123  // 返回 "number"
typeof "Hello World"  // 返回 "string"
typeof true  // 返回 "boolean"
typeof undefined  // 返回 "undefined"
typeof null  // 返回 "object"
typeof [1, 2, 3]  // 返回 "object"
typeof {"name": "小米", "age": 32}  // 返回 "object"
typeof function() {}  // 返回 "function"

Object.prototype.toString方法

Object.prototype.toString.call(123)  // 返回 "[object Number]"
Object.prototype.toString.call("Hello World")  // 返回 "[object String]"
Object.prototype.toString.call(true)  // 返回 "[object Boolean]"
Object.prototype.toString.call(undefined)  // 返回 "[object Undefined]"
Object.prototype.toString.call(null)  // 返回 "[object Null]"
Object.prototype.toString.call([1, 2, 3])  // 返回 "[object Array]"
Object.prototype.toString.call({"name": "小米", "age": 32})  // 返回 "[object Object]"
Object.prototype.toString.call(function() {})  // 返回 "[object Function]"

instanceof方法

var arr = [1, 2, 3];
console.log(arr instanceof Array);  // 返回 true
 
var obj = {"name": "小米", "age": 22};
console.log(obj instanceof Object);  // 返回 true
 
function Person(name, age) {
  this.name = name;
  this.age = age;
}
var alex = new Person("小米", 32);
console.log(alex instanceof Person);  // 返回 true

constructor方法

var arr = [1, 2, 3];
console.log(arr.constructor === Array);  // 返回 true
 
var obj = {"name": "小米", "age": 18};
console.log(obj.constructor === Object);  // 返回 true
 
function Person(name, age) {
  this.name = name;
  this.age = age;
}
var alex = new Person("小米", 18);
console.log(alex.constructor === Person);  // 返回 true

js用最简单的方法实现2个数组的交叉一对一合并

如何利用js语法将2个数组进行交叉合并

利用js语法把数组根据相同key转换成嵌套数组格式

javascript语法如何把json文件输出到html页面上

js点击按钮从服务器上下载json文件(不是打开文件)

标签: constructor方法, instanceof方法, Object.prototype.toString方法, typeof方法

上面是“如何利用js语法判断数据类型”的全面内容,想了解更多关于 js 内容,请继续关注web建站教程。

当前网址:https://m.ipkd.cn/webs_4791.html

声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

LogicFlow流程图框架使用示例
织梦dede只统计不显示点击次数,yes改no可不行
dedecms根据动态关键词获取对应文章(亲测有效)
帝国cms技巧之如何解决打开升级页面...e/update/i
如何利用php函数去除字符串右侧第一个字符