They say in JavaScript “everything is an object”. They’re wrong. Some types in JavaScript are so-called “primitive types”, and they don’t act like objects. These types are:
- Undefined
- Null
- Boolean
- Number
- String
The confusion comes from the fact that the boolean, number and string types can be treated like objects in a limited way. For example, the expression "I'm no object".length
returns the value 13
. This happens because when you attempt to access properties or methods on a primitive value, JavaScript instantiates a wrapper object temporarily, just so you can access its methods. ‘Cause JavaScript’s nice like that. I’m not going to go into more details here, but Angus Croll wrote about The Secret Life of JavaScript Primitives, so that would be a good place to learn more.
typeof
typeof
is a unary operator, just like the !
operator. It returns a string representing the type of its operand. Here are some examples:
typeof 3; // returns "number" typeof 'blah'; //returns "string" typeof {}; //returns "object" typeof []; //returns "object" typeof function () {}; //returns "function"
typeof
has its idiosyncrasies. For example, typeof null
returns "object"
, and typeof /[a-z]/
returns "function"
. Again, Angus Croll has written more on this subject than I have space for here.
So, basically typeof
is used for telling apart the different primitive types (as long as you don’t care about null
). It’s no use for telling different types of object apart though – for most objects typeof
will return "object"
.
constructor
constructor
is a property available on all objects’ prototypes, and it is a reference to the constructor function used to create the object. So, ({}).constructor
returns the Object
constructor function (the parentheses are needed to clarify a syntactic ambiguity) and[].constructor
returns the Array
constructor function. Likewise, it will return your custom constructor function:
function Person(name) {
this.name = name;
}
var dave = new Person('Dave');
dave.constructor === Person; //true
Remember that unlike the typeof
operator, constructor
returns a reference to the actual function. Another gotcha: because constructor
is part of the prototype, if you reassign the prototype to a constructor function, e.g. Person.prototype = {};
, you’ll lose the constructor
property.
instanceof
instanceof
is a binary operator – its syntax is instance instanceof Constructor
. So, to continue the above example:
dave instanceof Person; //true
The difference between instanceof
and the constructor
property (apart from the obvious syntactic difference) is that instanceof
inspects the object’s prototype chain. So, going back to our friend dave
again:
dave instanceof Object; //true
This is because Person.prototype
is an object, so Object
is in dave
‘s prototype chain, therefore dave
is an instance of Object
.
Wrap-up
So, if you’re dealing with primitive objects, use typeof
to distinguish them. Because typeof
returns "function"
for functions, it can also be useful for checking if an object member or a function argument is a function. If you’re working out the constructor of an object, use its constructor
property. And if you’re dealing with lengthy inheritance chains, and you want to find out whether an object inherits from a certain constructor, use instanceof
.
相关推荐
本文将深入探讨四种常用的方法来识别和判断JavaScript中的数据类型:`typeof`、`instanceof`、`constructor`以及`prototype`。 ### 1. `typeof` `typeof`操作符是最常见的类型检测方式之一,它返回一个表示未经计算...
在 JavaScript 中,typeof 和 instanceof 是两个常用的运算符,用来判断一个变量是否为空,或者是什么类型的。但是,这两个运算符之间还是有区别的。 typeof 运算符是一个一元运算符,放在一个运算数之前,运算数...
在JavaScript中,typeof和instanceof是常用的两种检测数据类型的方式,它们各自有其适用的场景和特点。 ### typeof typeof 是一个一元运算符,它用于返回变量或表达式的类型。当使用typeof运算符时,它通常会返回...
`typeof`、`instanceof`和`constructor`是JavaScript中三种常见的用于判断对象类型的工具。下面将详细阐述它们的联系、区别以及应用场景。 `typeof`操作符: `typeof`主要用于检测一个值的数据类型,返回的是一个...
避免使用`for in`循环遍历数组,采用经典`for`循环并缓存`length`,明确理解`Array`构造函数的行为,以及正确使用`typeof`和`instanceOf`进行类型检查,这些都是编写高质量JavaScript代码的关键。
在JavaScript中,`typeof`和`instanceof`是两种常用的类型检查操作符,它们各自有其独特的用法和局限性。 `typeof`操作符是一个一元运算符,它用于确定变量或表达式的类型,并返回一个表示该类型的字符串。`typeof`...
在JavaScript中,`typeof`和`instanceof`是两种常用的类型检查操作符,它们各自有着不同的用法和特点。理解这两者之间的差异对于编写健壮的JavaScript代码至关重要。 `typeof`操作符主要用于检测变量的数据类型。它...
在javascript中,typeof 和 instanceof 是用来判断数据类型比较通用的两个方法,这篇文章的目的是通过对这两个方法介绍来分析其存在的不足并提出优化方案。 typeof ——————————————————————...
使用方法如下:object instanceof constructor,object 为实例对象,constructor 为构造函数。 例如: let Car = function() {} let benz = new Car() benz instanceof Car // true let car = new String('xxx') ...
typeof用以获取一个变量的类型,typeof一般只能返回如下几个结果:number,boolean,string,function,object,undefined。我们可以使用typeof来获取一个变量是否存在,如 if(typeof a != “undefined”){},而不要去...
在JavaScript编程语言中,`instanceof` 和 `typeof` 是两个非常重要的操作符,用于判断变量的类型。它们各自有着不同的用途和用法,对于理解和调试代码至关重要。 首先,我们来详细了解一下`instanceof`操作符。`...
在JavaScript中,`typeof` 和 `instanceof` 都是用来检查变量类型的,但它们之间有着本质的区别。 `typeof` 是一个操作符,它返回一个表示变量类型的字符串。它主要用于基本数据类型的判断,例如: - number - ...
JavaScript constructor 属性在类型检查中的应用 在 JavaScript 中,constructor 属性是一种非常有用的工具,可以帮助我们检查变量的类型。Constructor 属性可以帮助我们解决 typeof 函数无法解决的问题,即无法...
`typeof`和`instanceof`是两种常见的用于检测数据类型的工具,但它们有着不同的用法和限制。下面我们将深入探讨这两个操作符的区别及其在实际开发中的应用。 1. `typeof` 操作符 `typeof` 是JavaScript中的一个...
在JavaScript中,`typeof`和`instanceof`是两种常用的类型检查操作符,它们都用于检测变量的类型,但有着不同的用法和返回结果。理解它们的区别对于编写健壮的JavaScript代码至关重要。 `typeof`是一个一元操作符,...
在JavaScript编程语言中,typeof和instanceof是两个用于检测数据类型的操作符。 typeof可以用来检测给定变量的基本数据类型,而instanceof用于检测对象是否属于某个特定的引用类型,或者更准确地说,是否是在某个类...