JavaScript中的instanceof运算符是什么? [英] What is the instanceof operator in JavaScript?
问题描述
JavaScript中的 instanceof
关键字在第一次遇到时会非常混乱,因为人们倾向于认为JavaScript不是面向对象的编程语言。
The instanceof
keyword in JavaScript can be quite confusing when it is first encountered, as people tend to think that JavaScript is not an object-oriented programming language.
- 这是什么?
- 它解决了什么问题?
- 什么时候合适,什么时候不合适?
推荐答案
instanceof
左手边(LHS)操作数是被测试到右手边(RHS)操作数的实际对象,它是类的实际构造函数。基本定义是:
instanceof
The Left Hand Side (LHS) operand is the actual object being tested to the Right Hand Side (RHS) operand which is the actual constructor of a class. The basic definition is:
Checks the current object and returns true if the object
is of the specified object type.
以下是好例子这里是一个直接来自 Mozilla的开发者网站:
Here are some good examples and here is an example taken directly from Mozilla's developer site:
var color1 = new String("green");
color1 instanceof String; // returns true
var color2 = "coral"; //no type specified
color2 instanceof String; // returns false (color2 is not a String object)
值得一提的是 instanceof
如果对象继承自classe的原型,则求值为true:
One thing worth mentioning is instanceof
evaluates to true if the object inherits from the classe's prototype:
var p = new Person("Jon");
p instanceof Person
那是 p instanceof Person
为真,因为 p
继承自 Person.prototype
。
That is p instanceof Person
is true since p
inherits from Person.prototype
.
我添加了一个带有一些示例代码和解释的小例子。
I've added a small example with some sample code and an explanation.
当你声明一个变量时,你给它一个特定的类型。
When you declare a variable you give it a specific type.
例如:
int i;
float f;
Customer c;
上面显示了一些变量,即 i
, f
, c
。类型是整数
, float
和用户定义客户
数据类型。上述类型可以用于任何语言,而不仅仅是JavaScript。但是,使用JavaScript声明变量时没有显式定义类型, var x
,x可以是数字/字符串/用户定义的数据类型。那么 instanceof
的作用是检查对象是否属于指定的类型,所以从上面取 Customer
我们可以做的对象:
The above show you some variables, namely i
, f
, and c
. The types are integer
, float
and a user defined Customer
data type. Types such as the above could be for any language, not just JavaScript. However, with JavaScript when you declare a variable you don't explicitly define a type, var x
, x could be a number / string / a user defined data type. So what instanceof
does is it checks the object to see if it is of the type specified so from above taking the Customer
object we could do:
var c = new Customer();
c instanceof Customer; //Returns true as c is just a customer
c instanceof String; //Returns false as c is not a string, it's a customer silly!
上面我们看到 c
是使用类型 Customer
声明。我们已经新了,并检查它是否是 Customer
类型。当然,它返回true。然后仍然使用 Customer
对象,我们检查它是否是 String
。不,绝对不是 String
我们新建了一个 Customer
对象而不是 String
对象。在这种情况下,它返回false。
Above we've seen that c
was declared with the type Customer
. We've new'd it and checked whether it is of type Customer
or not. Sure is, it returns true. Then still using the Customer
object we check if it is a String
. Nope, definitely not a String
we newed a Customer
object not a String
object. In this case, it returns false.
真的很简单!
这篇关于JavaScript中的instanceof运算符是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!