Atumọ typeof JavaScript

Atumọ typeof JavaScript

在 JavaScript 中有 5 种不同的可以包含值的数据类型:

  • string
  • number
  • boolean
  • object
  • function

有 6 种类型的对象:

  • Object
  • Date
  • Array
  • String
  • Number
  • Boolean

以及 2 种不能包含值的数据类型:

  • null
  • undefined

typeof 运算符

您可以使用 typeof 运算符来确定 JavaScript 变量的数据类型。

Ɗan

typeof "Bill"                 // 返回 "string"
typeof 3.14                   // 返回 "number"
typeof NaN                    // 返回 "number"
typeof false                  // 返回 "boolean"
typeof [1,2,3,4]              // 返回 "object"
typeof {name:'Bill', age:19}  // 返回 "object"
typeof new Date()             // 返回 "object"
typeof function () {}         // 返回 "function"
typeof myCar                  // 返回 "undefined" *
typeof null                   // 返回 "object"

Kara ƙo ƙirwa

请注意:

  • NaN 的数据类型是数字
  • 数组的数据类型是对象
  • 日期的数据类型是对象
  • null 的数据类型是 object
  • 未定义变量的数据类型为 undefined *
  • 未赋值的变量的数据类型也是 undefined *

您无法使用 typeof 来确定 JavaScript 对象是否是数组(或日期)。

原始日期

原始数据值指的是没有附加属性和方法的单个简单数据值。

typeof 运算符可以返回以下原始类型之一:

  • string
  • number
  • boolean
  • undefined

Ɗan

typeof "Bill"              // 返回 "string"
typeof 3.14                // 返回 "number"
typeof true                // 返回 "boolean"
typeof false               // 返回 "boolean"
typeof x                   // 返回 "undefined" (if x has no value)

Kara ƙo ƙirwa

复杂数据

typeof 运算符可以返回两种复杂类型之一:

  • function
  • object

typeof 运算符会为对象、数组和 null 返回 "object"。

typeof 运算符不会为函数返回 "object"。

Ɗan

typeof {name:'Bill', age:19} // 返回 "object"
typeof [1,2,3,4]             // 返回 "object"(非 "array",请注意下面的例子)
typeof null                  // 返回 "object"
typeof function myFunc(){}   // 返回 "function"

Kara ƙo ƙirwa

typeof 运算符会为数组返回 "object",因为在 JavaScript 中数组是对象。

typeof 的数据类型

typeof 运算符并不是变量。它只是一个运算符。运算符 (+ - * /) 没有任何数据类型。

但是,typeof 运算符总是返回字符串(包含操作数的类型)。

constructor 属性

constructor 属性返回所有 JavaScript 变量的构造函数。

Ɗan

"Bill".constructor                // 返回 function String()  {[native code]}
(3.14).constructor                // 返回 function Number()  {[native code]}
false.constructor                 // 返回 function Boolean() {[native code]}
[1,2,3,4].constructor             // Returns function Array()   {[native code]}
{name:'Bill',age:19}.constructor  // Returns function Object()  {[native code]}
new Date().constructor            // Returns function Date()    {[native code]}
function () {}.constructor        // Returns function Function(){[native code]}

Kara ƙo ƙirwa

You can check the constructor property to determine if the object is an array (including "Array" word):

Ɗan

function isArray(myArray) {
  return myArray.constructor.toString().indexOf("Array") > -1;
}

Kara ƙo ƙirwa

or more simply, you can check if the object isarray function:

Ɗan

function isArray(myArray) {
  return myArray.constructor === Array;
}

Kara ƙo ƙirwa

You can check the constructor property to determine if the object is a date (including "Date" word):

Ɗan

function isDate(myDate) {
  return myDate.constructor.toString().indexOf("Date") > -1;
}

Kara ƙo ƙirwa

or more simply, you can check if the object isdate function:

Ɗan

function isDate(myDate) {
  return myDate.constructor === Date;
}

Kara ƙo ƙirwa

Undefined

In JavaScript, the value of a variable without a value is undefined. The type is also undefinedis an object. The type should be

Ɗan

let car;    // Value is undefined, type is also undefined.

Kara ƙo ƙirwa

by setting its value to undefinedcan clear any variable. The type will also be undefinedis an object. The type should be

Ɗan

car = undefined;    // Value is undefined, type is also undefined.

Kara ƙo ƙirwa

null value

null value with undefined irrelevant.

An empty string has both a valid value and a type.

Ɗan

let car = "";    // Value is "", type is "string"

Kara ƙo ƙirwa

Null

in JavaScript null is 'null'. It should be something that does not exist.

Unfortunately, in JavaScript,null data type is an object.

你可以认为它是 JavaScript 中的一个 bug,You can consider it as a bug in JavaScript. typeof null nullis an object. The type should be

. null Ka ƙo ƙara ƙasa:

Ɗan

let person = {firstName:"Bill", lastName:"Gates", age:19, eyeColor:"blue"};
person = null;    // Now the value is null, but the type is still object

Kara ƙo ƙirwa

You can also set the object to undefined Ka ƙo ƙara ƙasa:

Ɗan

let person = {firstName:"Bill", lastName:"Gates", age:19, eyeColor:"blue"};
person = undefined;   // Now the value and type are both undefined

Kara ƙo ƙirwa

Differences between undefined and null

undefined da null Ƙarƙaƙa su ƙara, amma ana ƙara su ƙarƙaƙa:

typeof undefined           // undefined
typeof null                // object
null === undefined         // false
null == undefined          // true

Kara ƙo ƙirwa