Làm cách nào để tìm biến trong JavaScript?

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 (

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;

}

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)

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

javascript

// pi is not defined

pi; // throws ReferenceError

1 và

javascript

// pi is not defined

pi; // throws ReferenceError

2

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

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 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ới

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,

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 is not defined

pi; // throws ReferenceError

0)

Truy cập một biến không được xác định sẽ tạo ra một

javascript

// pi is not defined

pi; // throws ReferenceError

8

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

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;

}

6

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

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

Giá trị của một biến chưa được khởi tạo luôn là

javascript

// pi is not defined

pi; // throws ReferenceError

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;

}

9

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ử

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;

}

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;

}

68

Biểu thức

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;

}

69 không đưa ra giá trị

javascript

// pi is not defined

pi; // throws ReferenceError

8 nếu

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;

}

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 đị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;

}

2

Điều đó thật tuyệt vì bạn có thể sử dụng biểu thức

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;

}

72 để xác định xem biến có được xác định khô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;

}

4

Xin lưu ý rằ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;

}

72 đánh giá thà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;

}

74 khi

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;

}

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 is not defined

pi; // throws ReferenceError

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;

}

9

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ì

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;

}

72 ước tính thà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;

}

78

javascript

// pi is not defined

pi; // throws ReferenceError

2

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

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

Cho nên. còn việc bọc biến đã kiểm tra trong một khối

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;

}

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;

}

1

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;

}

71 trong ví dụ trên không được xác định. Khi cố gắng truy cập biến trong khối

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;

}

79, lỗi

javascript

// pi is not defined

pi; // throws ReferenceError

8 được đưa ra và khối

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;

}

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ến

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

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;

}

2

So với phương pháp

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;

}

60, phương pháp

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;

}

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ạo

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 (

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;

}

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ục

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;

}

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;

}

99

Ví dụ: đây là cách kiểm tra xem trình duyệt có xác định 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;

}

6 khô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;

}

3

Các biến

javascript

// pi is not defined

pi; // throws ReferenceError

0 và khai báo

javascript

// 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ục

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;

}

4

Tuy nhiên, hãy lưu ý rằng các 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;

}

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áo

javascript

// pi is not defined

pi; // throws ReferenceError

2, không tạo thuộc tính trên đối tượng toàn cầu

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;

}

5

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

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;

}

72 đánh giá thà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;

}

74 nếu

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;

}

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ông

Một cách tiếp cận khác là bọc biến trong khối

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;

}

29, sau đó bắt lỗi tham chiếu có thể xảy ra trong khối

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;

}

40. Nếu bạn bắt gặp một

javascript

// pi is not defined

pi; // throws ReferenceError

8, thì biến đó không được xác định

Cuối cùng, để kiểm tra sự tồn tại của biến toàn cục

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;

}

98, hãy gọi

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;

}

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ông

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?

Làm cách nào để tìm biến trong JavaScript?
đề xuất cải tiến

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 mua

Tham gia cùng 6719 người đăng ký khác

Làm cách nào để tìm biến trong JavaScript?

Làm cách nào để tìm biến trong JavaScript?

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 😉

Làm cách nào để xem tất cả các biến trong js?

Mở bảng điều khiển rồi nhập. .
keys(window) để xem các biến
dir(window) để xem các đối tượng

$_ trong JavaScript là gì?

Ký hiệu đô la ($) và các ký tự gạch dưới (_) là Các mã định danh JavaScript, điều này chỉ có nghĩa là chúng xác định một đối tượng theo cách giống như tên gọi. The objects they identify include things such as variables, functions, properties, events, and objects.

Nó là gì cho '$' trong JavaScript?

$ đại diện cho Hàm jQuery và thực sự là bí danh viết tắt của jQuery . (Không giống như hầu hết các ngôn ngữ, biểu tượng $ không được đặt trước và có thể được sử dụng làm tên biến. ) Nó thường được sử dụng như một bộ chọn (i. e. một hàm trả về một tập hợp các phần tử được tìm thấy trong DOM).

Làm cách nào để tìm loại dữ liệu trong JavaScript?

Bạn có thể sử dụng toán tử typeof để tìm loại dữ liệu của biến JavaScript.