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 了。
A 是 B 的父对象,c 是 B 的实例,c instanceof A 与 c instanceof B 结果均为 true。
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
演示 String 对象和 Date 对象都属于 Object 类型和一些特殊情况
下面的代码使用了 instanceof 来证明:String 和 Date 对象同时也属于Object 类型(他们是由 Object 类派生出来的)。
但是,使用对象文字符号创建的对象在这里是一个例外:虽然原型未定义,但 instanceof Object 返回 true。
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
Object.prototype.toString.call(object)/Object.prototype.toString.apply(object)
toString.call() 或 toString.apply() 方法几乎可以精准判断各类数据的类型。
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