Toán tử Một biểu thức đại diện cho đối tượng hoặc nguyên thủy có loại sẽ được trả về. Bảng sau đây tóm tắt các giá trị trả về có thể của Danh sách các giá trị này là đầy đủ. Không có động cơ tuân thủ cụ thể nào được báo cáo để sản xuất [hoặc có các giá trị được sản xuất trong lịch sử] ngoài các giá trị được liệt kê. Trình thám hiểm Internet cũ là trình duyệt duy nhất được biết là thực hiện các giá trị trả về bổ sung, trước khi thông số kỹ thuật loại bỏ hành vi của các chuỗi được xác định theo thực hiện typeof
trả về một chuỗi cho biết loại giá trị của toán hạng.typeof
operator returns a string indicating the type of the operand's value.Thử nó
Cú pháp
Thông số
operand
Sự mô tả
typeof
. Để biết thêm thông tin về các loại và nguyên thủy, hãy xem thêm trang cấu trúc dữ liệu JavaScript.Loại hìnhKết quả Chưa xác định
1// This stands since the beginning of JavaScript
typeof null === "object";
Vô giá trị
2 [lý do]// This stands since the beginning of JavaScript
typeof null === "object";
Boolean
3// This stands since the beginning of JavaScript
typeof null === "object";
Con số
4// This stands since the beginning of JavaScript
typeof null === "object";
Bigint
5// This stands since the beginning of JavaScript
typeof null === "object";
Sợi dây
6// This stands since the beginning of JavaScript
typeof null === "object";
Biểu tượng
7// This stands since the beginning of JavaScript
typeof null === "object";
Chức năng [thực hiện [[call]] trong các thuật ngữ ECMA-262; các lớp cũng là các chức năng]
8// This stands since the beginning of JavaScript
typeof null === "object";
Bất kỳ đối tượng khác
2// This stands since the beginning of JavaScript
typeof null === "object";
typeof
cho các đối tượng kỳ lạ không chuẩn.Ví dụ
Cách sử dụng cơ bản
// Numbers
typeof 37 === "number";
typeof 3.14 === "number";
typeof 42 === "number";
typeof Math.LN2 === "number";
typeof Infinity === "number";
typeof NaN === "number"; // Despite being "Not-A-Number"
typeof Number["1"] === "number"; // Number tries to parse things into numbers
typeof Number["shoe"] === "number"; // including values that cannot be type coerced to a number
typeof 42n === "bigint";
// Strings
typeof "" === "string";
typeof "bla" === "string";
typeof `template literal` === "string";
typeof "1" === "string"; // note that a number within a string is still typeof string
typeof typeof 1 === "string"; // typeof always returns a string
typeof String[1] === "string"; // String converts anything into a string, safer than toString
// Booleans
typeof true === "boolean";
typeof false === "boolean";
typeof Boolean[1] === "boolean"; // Boolean[] will convert values based on if they're truthy or falsy
typeof !!1 === "boolean"; // two calls of the ! [logical NOT] operator are equivalent to Boolean[]
// Symbols
typeof Symbol[] === "symbol";
typeof Symbol["foo"] === "symbol";
typeof Symbol.iterator === "symbol";
// Undefined
typeof undefined === "undefined";
typeof declaredButUndefinedVariable === "undefined";
typeof undeclaredVariable === "undefined";
// Objects
typeof { a: 1 } === "object";
// use Array.isArray or Object.prototype.toString.call
// to differentiate regular objects from arrays
typeof [1, 2, 4] === "object";
typeof new Date[] === "object";
typeof /regex/ === "object";
// The following are confusing, dangerous, and wasteful. Avoid them.
typeof new Boolean[true] === "object";
typeof new Number[1] === "object";
typeof new String["abc"] === "object";
// Functions
typeof function [] {} === "function";
typeof class C {} === "function";
typeof Math.sin === "function";
Loại null
// This stands since the beginning of JavaScript
typeof null === "object";
Trong lần thực hiện đầu tiên của JavaScript, các giá trị JavaScript được biểu diễn dưới dạng thẻ loại và giá trị. Thẻ loại cho các đối tượng là
const str = new String["String"];
const num = new Number[100];
typeof str; // "object"
typeof num; // "object"
const func = new Function[];
typeof func; // "function"
1. const str = new String["String"];
const num = new Number[100];
typeof str; // "object"
typeof num; // "object"
const func = new Function[];
typeof func; // "function"
2 được biểu diễn dưới dạng con trỏ null [const str = new String["String"];
const num = new Number[100];
typeof str; // "object"
typeof num; // "object"
const func = new Function[];
typeof func; // "function"
3 trong hầu hết các nền tảng]. Do đó, const str = new String["String"];
const num = new Number[100];
typeof str; // "object"
typeof num; // "object"
const func = new Function[];
typeof func; // "function"
2 có const str = new String["String"];
const num = new Number[100];
typeof str; // "object"
typeof num; // "object"
const func = new Function[];
typeof func; // "function"
1 dưới dạng thẻ loại, do đó giá trị trả về typeof
// This stands since the beginning of JavaScript
typeof null === "object";
2. [tài liệu tham khảo]Một bản sửa lỗi đã được đề xuất cho Ecmascript [thông qua chọn tham gia], nhưng đã bị từ chối. Nó sẽ dẫn đến
const str = new String["String"];
const num = new Number[100];
typeof str; // "object"
typeof num; // "object"
const func = new Function[];
typeof func; // "function"
8.Sử dụng toán tử mới
Tất cả các hàm của hàm tạo được gọi với
const str = new String["String"];
const num = new Number[100];
typeof str; // "object"
typeof num; // "object"
const func = new Function[];
typeof func; // "function"
9 sẽ trả về những người không theo nguyên tắc [// This stands since the beginning of JavaScript
typeof null === "object";
2 hoặc // This stands since the beginning of JavaScript
typeof null === "object";
8]. Hầu hết các đối tượng trả về, với ngoại lệ đáng chú ý là // Parentheses can be used for determining the data type of expressions.
const someData = 99;
typeof someData + " Wisen"; // "number Wisen"
typeof [someData + " Wisen"]; // "string"
2, trả về một hàm.const str = new String["String"];
const num = new Number[100];
typeof str; // "object"
typeof num; // "object"
const func = new Function[];
typeof func; // "function"
Cần cho dấu ngoặc trong cú pháp
Toán tử typeof
có ưu tiên cao hơn các toán tử nhị phân như Bổ sung [
// Parentheses can be used for determining the data type of expressions.
const someData = 99;
typeof someData + " Wisen"; // "number Wisen"
typeof [someData + " Wisen"]; // "string"
4]. Do đó, dấu ngoặc đơn là cần thiết để đánh giá loại kết quả bổ sung.// Parentheses can be used for determining the data type of expressions.
const someData = 99;
typeof someData + " Wisen"; // "number Wisen"
typeof [someData + " Wisen"]; // "string"
Tương tác với các biến không được khai báo và không được công khai
typeof
thường luôn được đảm bảo trả lại một chuỗi cho bất kỳ toán hạng nào mà nó được cung cấp. Ngay cả với các định danh không được khai báo, typeof
sẽ trả về
// This stands since the beginning of JavaScript
typeof null === "object";
1 thay vì ném lỗi.typeof undeclaredVariable; // "undefined"
Tuy nhiên, sử dụng typeof
trên các khai báo từ vựng [
// Parentheses can be used for determining the data type of expressions.
const someData = 99;
typeof someData + " Wisen"; // "number Wisen"
typeof [someData + " Wisen"]; // "string"
9 typeof undeclaredVariable; // "undefined"
0 và typeof undeclaredVariable; // "undefined"
1] trong cùng một khối trước khi dòng khai báo sẽ ném typeof undeclaredVariable; // "undefined"
2. Các biến phạm vi khối nằm trong vùng chết tạm thời từ khi bắt đầu khối cho đến khi khởi tạo được xử lý, trong đó nó sẽ gây ra lỗi nếu được truy cập.typeof newLetVariable; // ReferenceError
typeof newConstVariable; // ReferenceError
typeof newClass; // ReferenceError
let newLetVariable;
const newConstVariable = "hello";
class newClass {}
Hành vi đặc biệt của tài liệu. Tất cả
Tất cả các trình duyệt hiện tại hiển thị một đối tượng máy chủ không chuẩn
typeof undeclaredVariable; // "undefined"
3 với loại typeof undeclaredVariable; // "undefined"
4.typeof document.all === "undefined";
Mặc dù
typeof undeclaredVariable; // "undefined"
3 cũng giả mạo và lỏng lẻo bằng typeof undeclaredVariable; // "undefined"
4, nhưng nó không phải là typeof undeclaredVariable; // "undefined"
4. Trường hợp của typeof undeclaredVariable; // "undefined"
3 có loại // This stands since the beginning of JavaScript
typeof null === "object";
1 được phân loại trong các tiêu chuẩn web là "vi phạm cố ý" của tiêu chuẩn ECMAscript ban đầu cho khả năng tương thích web.Phương pháp tùy chỉnh có loại cụ thể hơn
typeof
rất hữu ích, nhưng nó không linh hoạt như có thể được yêu cầu. Ví dụ,
typeof newLetVariable; // ReferenceError
typeof newConstVariable; // ReferenceError
typeof newClass; // ReferenceError
let newLetVariable;
const newConstVariable = "hello";
class newClass {}
1 là // This stands since the beginning of JavaScript
typeof null === "object";
2, cũng như typeof newLetVariable; // ReferenceError
typeof newConstVariable; // ReferenceError
typeof newClass; // ReferenceError
let newLetVariable;
const newConstVariable = "hello";
class newClass {}
3, typeof newLetVariable; // ReferenceError
typeof newConstVariable; // ReferenceError
typeof newClass; // ReferenceError
let newLetVariable;
const newConstVariable = "hello";
class newClass {}
4, v.v.Để biết độ đặc hiệu cao hơn trong các loại kiểm tra, ở đây chúng tôi trình bày hàm
typeof newLetVariable; // ReferenceError
typeof newConstVariable; // ReferenceError
typeof newClass; // ReferenceError
let newLetVariable;
const newConstVariable = "hello";
class newClass {}
5 tùy chỉnh, chủ yếu bắt chước hành vi của typeof
, nhưng đối với các đối tượng và chức năng không chính đáng [nghĩa là nó sẽ trả về một tên loại chi tiết hơn có thể.function type[value] {
if [value === null] {
return "null";
}
const baseType = typeof value;
// Primitive types
if [!["object", "function"].includes[baseType]] {
return baseType;
}
// Symbol.toStringTag often specifies the "display name" of the
// object's class. It's used in Object.prototype.toString[].
const tag = value[Symbol.toStringTag];
if [typeof tag === "string"] {
return tag;
}
// If it's a function whose source code starts with the "class" keyword
if [
baseType === "function" &&
Function.prototype.toString.call[value].startsWith["class"]
] {
return "class";
}
// The name of the constructor; for example `Array`, `GeneratorFunction`,
// `Number`, `String`, `Boolean` or `MyCustomClass`
const className = value.constructor.name;
if [typeof className === "string" && className !== ""] {
return className;
}
// At this point there's no robust way to get the type of value,
// so we use the base implementation.
return baseType;
}
Để kiểm tra các biến có khả năng không tồn tại mà nếu không sẽ ném
typeof undeclaredVariable; // "undefined"
2, hãy sử dụng typeof newLetVariable; // ReferenceError
typeof newConstVariable; // ReferenceError
typeof newClass; // ReferenceError
let newLetVariable;
const newConstVariable = "hello";
class newClass {}
8 vì hành vi này không thể được bắt chước với mã tùy chỉnh.Thông số kỹ thuật
Đặc tả ngôn ngữ Ecmascript # Sec-TypeOf-Coperator # sec-typeof-operator |
Tính tương thích của trình duyệt web
Bảng BCD chỉ tải trong trình duyệt