openplanning

Toán tử typeof trong TypeScript

Xem thêm các chuyên mục:

Nhóm phát triển của chúng tôi vừa ra mắt website langlearning.net học tiếng Anh, Nga, Đức, Pháp, Việt, Trung, Hàn, Nhật, ... miễn phí cho tất cả mọi người.
Là một website được viết trên công nghệ web Flutter vì vậy hỗ trợ rất tốt cho người học, kể cả những người học khó tính nhất.
Hiện tại website đang tiếp tục được cập nhập nội dung cho phong phú và đầy đủ hơn. Mong các bạn nghé thăm và ủng hộ website mới của chúng tôi.
Hãy theo dõi chúng tôi trên Fanpage để nhận được thông báo mỗi khi có bài viết mới. Facebook

1- Toán tử typeof

Toán tử typeof trả về một string biểu thị kiểu dữ liệu của một giá trị được đánh giá.
Cú pháp:

let result = typeof aValue;
Ví dụ:
typeof_ex1.ts

console.log(typeof 42); // "number"

console.log(typeof 'blubber'); // "string"

console.log(typeof true); // "boolean

let aVariable; // A variable is not initialized.
console.log(typeof aVariable); // "undefined"
Về cơ bản, toán tử typeof chỉ trả về một trong các giá trị sau đây:
No Type Returns
1 undefined "undefined"
2 null "object"
3 Boolean "boolean"
4 number "number"
5 BigInt "bigint"
6 string "string"
7 Symbol "symbol"
8 Function object "function"
9 Any other object "object"
Ví dụ:
typeof_ex2.ts

console.log(' --- (1) typeof undefined -- ');
console.log(typeof undefined); //
let aValue; // A variable is not initialized
console.log(typeof aValue);  // "undefined"

console.log(' --- (2) typeof null -- ');
console.log(typeof null); // "object"

console.log(' --- (3) typeof Boolean -- ');
console.log(typeof true); // "boolean"
console.log(typeof (3 > 5)); // "boolean"

console.log(' --- (4) typeof Number -- ');
console.log(typeof 100); // "number"

console.log(' --- (5) typeof bigint -- ');
console.log(' ** See the bigint article for how to use the bigint library.');

console.log(' --- (6) typeof string -- ');
console.log(typeof "Tom"); // "string"

console.log(' --- (7) typeof Symbol -- ');
let aSymbol = Symbol(123);
console.log(typeof aSymbol); // "symbol"

console.log(' --- (8) typeof Function (or Closure) -- ');
let aFunction = function() {
    console.log('Hello');
};
console.log(typeof aFunction); // "function"

console.log(' --- (8) typeof Any-Other-Object -- ');
var anObject1 = {name: 'Tom', salary: 1000};
var anObject2 = new Object();
console.log(typeof anObject1); // "object"
console.log(typeof anObject2); // "object"
Output:

--- (1) typeof undefined --
undefined
undefined
--- (2) typeof null --
object
--- (3) typeof Boolean --
boolean
boolean
--- (4) typeof Number --
number
--- (5) typeof bigint --
** See the bigint article for how to use the bigint library.
--- (6) typeof string --
string
--- (7) typeof Symbol --
symbol
--- (8) typeof Function (or Closure) --
function
--- (8) typeof Any-Other-Object --
object
object
Ví dụ: Sử dụng toán tử typeof để xác định kiểu dữ liệu đã truyền vào một constructor:
typeof_ex3.ts

class Song {
    title: string;
    duration: string; // "minutes:seconds".
     
    constructor(title: string, duration: string | number) {
        this.title = title;
        if(typeof duration === 'string') {
            this.duration = duration;
        } else {
            let seconds = duration % 60;
            let minutes = Math.floor(duration / 60);
            this.duration = minutes + ":" + seconds;
        }
    }
}
let mySong1 = new Song("Hotel California", "6:30");
console.log(`mySong1.duration = ${mySong1.duration}`); // 6:30

let mySong2 = new Song("My Heart Will Go On", 270); // seconds
console.log(`mySong2.duration = ${mySong2.duration}`); // 4:30
Output:

mySong1.duration = 6:30
mySong2.duration = 4:30

2- Toán tử instanceof

Xem thêm các chuyên mục: