Thỉnh thoảng bạn phải kiểm tra xem một biến có được định nghĩa trong JavaScript hay không. Ví dụ: để xác định xem tập lệnh bên ngoài đã được tải thành công vào trang web chưa hoặc để xác định xem trình duyệt có hỗ trợ API Web hay không [
6,javascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
7]javascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
Làm cách nào để kiểm tra xem một biến có được xác định trong JavaScript không?
1. Các trạng thái của biến
Trước khi chuyển sang các kỹ thuật cụ thể, tôi muốn có một thỏa thuận về các điều khoản liên quan
Trong 2 phần sau đây, chúng ta hãy làm rõ ý nghĩa của việc một biến được "xác định"/"không xác định" và "khởi tạo"/"chưa khởi tạo"
1. 1 Biến xác định / không xác định
Một biến được định nghĩa khi nó đã được khai báo trong phạm vi hiện tại bằng cách sử dụng câu lệnh khai báo
Cách khai báo biến thông thường là các câu lệnh ______ 08, _______ 09 và ________ 40, cộng với các câu lệnh khai báo
1 vàjavascript
// pi is not defined
pi; // throws ReferenceError
2javascript
// pi is not defined
pi; // throws ReferenceError
Ví dụ về các biến được xác định
javascript
const pi = 3.14; // pi is defined
let result; // result is defined
window.message = 'Hello';
message; // message is defined
Ngược lại, một biến không được định nghĩa khi nó chưa được khai báo trong phạm vi hiện tại bằng cách sử dụng câu lệnh khai báo
Ví dụ về các biến không được xác định
javascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
Phạm vi đặt giới hạn nơi biến được xác định và có thể truy cập. Một phạm vi trong JavaScript được xác định bởi một khối mã [đối với các biến
8 vàjavascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
9] và bởi một thân hàm [đối vớijavascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
8,javascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
9,javascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
0]javascript
// pi is not defined
pi; // throws ReferenceError
Truy cập một biến không được xác định sẽ tạo ra một
8javascript
// pi is not defined
pi; // throws ReferenceError
javascript
// pi is not defined
pi; // throws ReferenceError
1. 2 Biến khởi tạo/chưa khởi tạo
Một biến được khởi tạo khi biến được khai báo đã được gán giá trị ban đầu
Ví dụ về các biến khởi tạo
6javascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
Ngược lại, một biến không được khởi tạo khi biến được khai báo chưa được gán giá trị ban đầu
Ví dụ về các biến chưa được khởi tạo
7javascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
Giá trị của một biến chưa được khởi tạo luôn là
9javascript
// pi is not defined
pi; // throws ReferenceError
9javascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
2. sử dụng loại
Biết các trạng thái có thể có của biến, hãy xem xét các kỹ thuật để tìm xem một biến có xác định hay không
Toán tử
60 xác định kiểu của biến.javascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
61 có thể đánh giá một trong các giá trị.javascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
62,javascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
63,javascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
64,javascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
65,javascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
66,javascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
67 vàjavascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
68javascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
Biểu thức
69 không đưa ra giá trịjavascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
8 nếujavascript
// pi is not defined
pi; // throws ReferenceError
71 không được xác định, trái với cách truy cập đơn giản của biến không được xác địnhjavascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
2javascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
Điều đó thật tuyệt vì bạn có thể sử dụng biểu thức
72 để xác định xem biến có được xác định khôngjavascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
4javascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
Xin lưu ý rằng
72 đánh giá thànhjavascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
74 khijavascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
75 không được xác định, nhưng cả khi được xác định và chưa được khởi tạo. Tất cả chỉ vì việc truy cập vào một biến được xác định nhưng chưa được khởi tạo có giá trị làjavascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
9javascript
// pi is not defined
pi; // throws ReferenceError
9javascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
Thông thường, đó không phải là một vấn đề. Khi bạn kiểm tra xem biến có được xác định hay không, bạn cũng muốn nó được khởi tạo với tải trọng
Tất nhiên, nếu biến được xác định và có giá trị, thì
72 ước tính thànhjavascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
78javascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
2javascript
// pi is not defined
pi; // throws ReferenceError
3. Sử dụng thử/bắt
Khi truy cập một biến không được xác định, JavaScript sẽ đưa ra lỗi tham chiếu
0javascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
Cho nên. còn việc bọc biến đã kiểm tra trong một khối
79 và cố gắng bắt lỗi tham chiếu thì sao?javascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
1javascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
71 trong ví dụ trên không được xác định. Khi cố gắng truy cập biến trong khốijavascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
79, lỗijavascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
8 được đưa ra và khốijavascript
// pi is not defined
pi; // throws ReferenceError
93 bắt gặp lỗi tham chiếu này. Đó là một cách khác để kiểm tra sự tồn tại của biếnjavascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
Tất nhiên, nếu biến được xác định, thì không có lỗi tham chiếu nào được đưa ra
2javascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
So với phương pháp
60, phương phápjavascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
95 chính xác hơn vì nó chỉ xác định nếu biến không được xác định, bất kể được khởi tạo hay chưa khởi tạojavascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
4. Sử dụng cửa sổ. hasOwnProperty[]
Cuối cùng, để kiểm tra sự tồn tại của các biến toàn cục, bạn có thể thực hiện một cách tiếp cận đơn giản hơn
Mỗi biến toàn cục được lưu trữ dưới dạng một thuộc tính trên đối tượng toàn cục [
96 trong môi trường trình duyệt,javascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
97 trong NodeJS]. Bạn có thể sử dụng ý tưởng này để xác định xem biến toàn cụcjavascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
98 có được xác định hay không. chỉ cần kiểm tra đối tượng toàn cầu để biết sự tồn tại của thuộc tính tương ứng.javascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
99javascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
Ví dụ: đây là cách kiểm tra xem trình duyệt có xác định biến
6 khôngjavascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
3javascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
Các biến
0 và khai báojavascript
// pi is not defined
pi; // throws ReferenceError
1, khi được sử dụng trong phạm vi ngoài cùng [còn gọi là phạm vi toàn cầu], sẽ tạo các thuộc tính trên đối tượng toàn cụcjavascript
// pi is not defined
pi; // throws ReferenceError
4javascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
Tuy nhiên, hãy lưu ý rằng các biến
8 vàjavascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
9, cũng như khai báojavascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
2, không tạo thuộc tính trên đối tượng toàn cầujavascript
// pi is not defined
pi; // throws ReferenceError
5javascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
5. Tóm lược
Trong JavaScript, một biến có thể được xác định hoặc không được xác định, cũng như được khởi tạo hoặc chưa khởi tạo
72 đánh giá thànhjavascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
74 nếujavascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
75 không được xác định, nhưng cũng được xác định và chưa được khởi tạo. Đó là một cách nhanh chóng để xác định xem một biến có được xác định hay khôngjavascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
Một cách tiếp cận khác là bọc biến trong khối
29, sau đó bắt lỗi tham chiếu có thể xảy ra trong khốijavascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
40. Nếu bạn bắt gặp mộtjavascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
8, thì biến đó không được xác địnhjavascript
// pi is not defined
pi; // throws ReferenceError
Cuối cùng, để kiểm tra sự tồn tại của biến toàn cục
98, hãy gọijavascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
99. Cách tiếp cận này rất hữu ích để kiểm tra xem trình duyệt có hỗ trợ API Web khôngjavascript
pi; // pi is not defined, throws ReferenceError
result; // result is not defined, throws ReferenceError
if [true] {
// result is defined, but in this block scope
let result;
}
Cách ưa thích của bạn để kiểm tra nếu một biến được xác định là gì?
Thích bài viết?
Bài đăng chất lượng vào hộp thư đến của bạn
Tôi thường xuyên xuất bản bài viết có chứa
- Các khái niệm JavaScript quan trọng được giải thích bằng các từ đơn giản
- Tổng quan về các tính năng JavaScript mới
- Cách sử dụng TypeScript và cách gõ
- Thiết kế phần mềm và thực hành mã hóa tốt
Đăng ký nhận bản tin của tôi để nhận chúng ngay trong hộp thư đến của bạn
Đặt muaTham gia cùng 6719 người đăng ký khác
Giới thiệu về Dmitri Pavlutin
Nhà văn và huấn luyện viên công nghệ. Thói quen hàng ngày của tôi bao gồm [nhưng không giới hạn] uống cà phê, viết mã, viết, huấn luyện, vượt qua sự nhàm chán 😉