本文共 662 字,大约阅读时间需要 2 分钟。
instanceof运算符
The JavaScript instanceof
operator returns true if the first operand is an instance of the object passed on the right, or one of its ancestors in its prototype chain.
如果第一个操作数是在右侧传递的对象的实例或其原型链中的祖先之一,则JavaScript instanceof
运算符将返回true。
In this example you can see that the myCar
object, of class Fiesta, responds true to instanceof Fiesta
, and also responds true to instanceOf Car
, because Fiesta extends Car:
在此示例中,您可以看到Fiesta类的myCar
对象对Fiesta的instanceof Fiesta
响应为true,并且对instanceOf Car
响应为true,因为Fiesta扩展了Car:
class Car {}class Fiesta extends Car {}const myCar = new Fiesta()myCar instanceof Fiesta //truemyCar instanceof Car //true
翻译自:
instanceof运算符
转载地址:http://aaqgb.baihongyu.com/