首页 > 社交 > 科普中国

JavaScript中的数据类型判断

常驻编辑 科普中国 2022-06-13 数据类型   数组   原型   字符串   函数   属性   对象   参数   类型   方法
function Car(make, model, year) { this.make = make; this.model = model; this.year = year; } const auto = new Car('Honda', 'Accord', 1998); console.log(auto instanceof Car); // expected output: true console.log(auto instanceof Object); // expected output: true
// 定义构造函数
function C(){}
function D(){}

var o = new C();

o instanceof C; // true,因为 Object.getPrototypeOf(o) === C.prototype

o instanceof D; // false,因为 D.prototype 不在 o 的原型链上

o instanceof Object; // true,因为 Object.prototype.isPrototypeOf(o) 返回 true
C.prototype instanceof Object // true,同上

C.prototype = {};
var o2 = new C();

o2 instanceof C; // true

o instanceof C; // false,C.prototype 指向了一个空对象,这个空对象不在 o 的原型链上.

D.prototype = new C(); // 继承
var o3 = new D();
o3 instanceof D; // true
o3 instanceof C; // true 因为 C.prototype 现在在 o3 的原型链上

需要注意的是,如果表达式 obj instanceof Foo 返回 true,则并不意味着该表达式会永远返回 true,因为 Foo.prototype 属性的值有可能会改变,改变之后的值很有可能不存在于 obj 的原型链上,这时原表达式的值就会成为 false。另外一种情况下,原表达式的值也会改变,就是改变对象 obj 的原型链的情况,虽然在目前的ES规范中,我们只能读取对象的原型而不能改变它,但借助于非标准的 __proto__ 伪属性,是可以实现的。比如执行 obj.__proto__ = {} 之后,obj instanceof Foo 就会返回 false 了。Yfh拜客生活常识网


Yfh拜客生活常识网

A 是 B 的父对象,c 是 B 的实例,c instanceof A 与 c instanceof B 结果均为 true。Yfh拜客生活常识网

function A() { }
function B() { }
B.prototype = new A()
const c = new B()

console.log(c instanceof B); // true
console.log(c instanceof A); // true
console.log(A instanceof B); // false
console.log(B instanceof A); // false
console.log(B.__proto__ === A.prototype); // false
console.log(B.prototype.__proto__ === A.prototype); // true
console.log(c instanceof Object); // true c 属于
console.log(B instanceof Object); // true
console.log(A instanceof Object); // true


Yfh拜客生活常识网

演示 String 对象和 Date 对象都属于 Object 类型和一些特殊情况

下面的代码使用了 instanceof 来证明:String 和 Date 对象同时也属于Object 类型(他们是由 Object 类派生出来的)。Yfh拜客生活常识网

但是,使用对象文字符号创建的对象在这里是一个例外:虽然原型未定义,但 instanceof Object 返回 true。Yfh拜客生活常识网

var simpleStr = "This is a simple string";
var myString  = new String();
var newStr    = new String("String created with constructor");
var myDate    = new Date();
var myObj     = {};
var myNonObj  = Object.create(null);

console.log(simpleStr instanceof String) // 返回 false, 非对象实例,因此返回 false
console.log(myString  instanceof String) // 返回 true
console.log(newStr    instanceof String) // 返回 true
console.log(myString  instanceof Object) // 返回 true

console.log(myObj instanceof Object)     // 返回 true, 尽管原型没有定义
console.log(({})  instanceof Object)     // 返回 true, 同上
console.log(myNonObj instanceof Object)  // 返回 false, 一种创建非 Object 实例的对象的方法

console.log(myString instanceof Date)    // 返回 false

console.log(myDate instanceof Date)      // 返回 true
console.log(myDate instanceof Object)    // 返回 true
console.log(myDate instanceof String)    // 返回 false


Yfh拜客生活常识网


Yfh拜客生活常识网

Object.prototype.toString.call(object)/Object.prototype.toString.apply(object)

toString.call() 或 toString.apply() 方法几乎可以精准判断各类数据的类型。Yfh拜客生活常识网

console.log(Object.prototype.toString.call("kevin"))   // [object String]
console.log(Object.prototype.toString.call(18))        // [object Number]
console.log(Object.prototype.toString.call(true))      // [object Boolean]
console.log(Object.prototype.toString.call(undefined)) // [object Undefined]
console.log(Object.prototype.toString.call(null))      // [object Null]
console.log(Object.prototype.toString.call(NaN))       // [object Number]
console.log(Object.prototype.toString.call({ name: "kevin" })) // [object Object]
console.log(Object.prototype.toString.call(function () { }))   // [object Function]
console.log(Object.prototype.toString.call([]))                // [object Array]
console.log(Object.prototype.toString.call(new Date))          // [object Date]
console.log(Object.prototype.toString.call(/d/))              // [object RegExp]
console.log(Object.prototype.toString.call(Math))              // [object Math]
function Person() { }
console.log(Object.prototype.toString.call(new Person))        // [object Object]
var o = { [Symbol.toStringTag]: "A" }
console.log(Object.prototype.toString.call(o))                 // [object A]
console.log(window.toString())                                 // "[object Window]"
console.log(Object.prototype.toString.call(window))            // "[object Window]"
console.log(Object.prototype.toString.call(Symbol()));         // "[object Symbol]"

// 封装
function getTypeof(data) {
  let dataType = Object.prototype.toString.call(data);
  return dataType.slice(8, -1)
}
console.log(getTypeof(18)) // Number
console.log(getTypeof("kevin")) // String
console.log(getTypeof(new Date)) // Date
console.log(getTypeof([])) // Array
console.log(getTypeof({})) // Object
function Person() { }
console.log(getTypeof(new Person)) // Object
console.log(getTypeof(new Person())) // Object    

相关阅读:

  • 设计模式之原型模式
  • Python八大数据类型
  • go语言如何入门呢?
  • 介绍一下西门子S7300PLC的S5定时器的S5TIME数据格式
  • 如何输入数组(如何将数字输入数组)
  • 指针怎么用 c语言指针与一维数组?
  • java
  • 集合篇
  • GO学习
  • 技巧篇:常用的vba代码汇总
    • 网站地图 |
    • 声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。文章内容仅供参考,不做权威认证,如若验证其真实性,请咨询相关权威专业人士。