Hướng dẫn why null in javascript is object? - tại sao null trong javascript là đối tượng?

Giá trị

// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
8 đại diện cho sự vắng mặt có chủ ý của bất kỳ giá trị đối tượng nào. Đây là một trong những giá trị nguyên thủy của JavaScript và được coi là giả cho các hoạt động Boolean.

Thử nó

Cú pháp

Sự mô tả

Giá trị

// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
8 được viết bằng chữ:
// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
8.
// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
8 không phải là một định danh cho một thuộc tính của đối tượng toàn cầu, như
typeof null          // "object" [not "null" for legacy reasons]
typeof undefined     // "undefined"
null === undefined   // false
null  == undefined   // true
null === null        // true
null  == null        // true
!null                // true
isNaN[1 + null]      // false
isNaN[1 + undefined] // true
2 có thể. Thay vào đó,
// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
8 thể hiện sự thiếu nhận dạng, chỉ ra rằng một biến chỉ ra không có đối tượng. Trong API,
// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
8 thường được lấy ở một nơi có thể mong đợi một đối tượng nhưng không có đối tượng nào có liên quan.

// foo does not exist. It is not defined and has never been initialized:
foo; //ReferenceError: foo is not defined

// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null

Ví dụ

Sự khác biệt giữa
// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
8 và
typeof null          // "object" [not "null" for legacy reasons]
typeof undefined     // "undefined"
null === undefined   // false
null  == undefined   // true
null === null        // true
null  == null        // true
!null                // true
isNaN[1 + null]      // false
isNaN[1 + undefined] // true
2

Khi kiểm tra

// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
8 hoặc
typeof null          // "object" [not "null" for legacy reasons]
typeof undefined     // "undefined"
null === undefined   // false
null  == undefined   // true
null === null        // true
null  == null        // true
!null                // true
isNaN[1 + null]      // false
isNaN[1 + undefined] // true
2, hãy cẩn thận với sự khác biệt giữa các toán tử bình đẳng [==] và danh tính [===], vì trước đây thực hiện chuyển đổi loại.

typeof null          // "object" [not "null" for legacy reasons]
typeof undefined     // "undefined"
null === undefined   // false
null  == undefined   // true
null === null        // true
null  == null        // true
!null                // true
isNaN[1 + null]      // false
isNaN[1 + undefined] // true

Thông số kỹ thuật

Sự chỉ rõ
Đặc tả ngôn ngữ Ecmascript # sec-null-giá trị
# sec-null-value

Tính tương thích của trình duyệt web

Bảng BCD chỉ tải trong trình duyệt

Xem thêm

JavaScript có 2 loại: nguyên thủy [chuỗi, booleans, số, ký hiệu] và đối tượng.

Đối tượng là cấu trúc dữ liệu phức tạp. Đối tượng đơn giản nhất trong JavaScript là đối tượng thuần túy - một tập hợp các khóa và giá trị liên quan:

javascript

let myObject = {

name: 'Eric Cartman'

};

Nhưng có những tình huống khi một đối tượng không thể được tạo ra. Đối với những trường hợp như vậy, JavaScript cung cấp một giá trị đặc biệt

// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
8 - cho biết một đối tượng bị thiếu.

javascript

let myObject = null;

Trong bài đăng này, bạn sẽ tìm hiểu mọi thứ về

// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
8 trong JavaScript: ý nghĩa của nó, cách phát hiện nó, sự khác biệt giữa
// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
8 và
typeof null          // "object" [not "null" for legacy reasons]
typeof undefined     // "undefined"
null === undefined   // false
null  == undefined   // true
null === null        // true
null  == null        // true
!null                // true
isNaN[1 + null]      // false
isNaN[1 + undefined] // true
2 và tại sao sử dụng
// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
8 tạo ra các khó khăn bảo trì mã.

Mục lục

  • 1. Khái niệm về NULL
    • 1.1 Tương tự thế giới thực của NULL
  • 2. Cách kiểm tra NULL
    • 2.1 null là giả mạo
    • 2.2 Loại null
  • 3. cái bẫy của null
  • 4. Các lựa chọn thay thế cho null
  • 5. null vs không xác định
  • 6. Tóm tắt

1. Khái niệm về NULL

1.1 Tương tự thế giới thực của NULL

2. Cách kiểm tra NULL

2.1 null là giả mạo

2.2 Loại null

javascript

function greetObject[who] {

if [!who] {

return null;

}

return { message: `Hello, ${who}!` };

}

greetObject['Eric']; // => { message: 'Hello, Eric!' }

greetObject[]; // => null

3. cái bẫy của null

4. Các lựa chọn thay thế cho null

1.1 Tương tự thế giới thực của NULL

2. Cách kiểm tra NULL

2.1 null là giả mạo

2. Cách kiểm tra NULL

2.1 null là giả mạo

javascript

const missingObject = null;

const existingObject = { message: 'Hello!' };

missingObject === null; // => true

existingObject === null; // => false

2.2 Loại null

Nếu biến chứa một giá trị không null, giống như một đối tượng, biểu thức

javascript

function greetObject[who] {

if [!who] {

return null;

}

return { message: `Hello, ${who}!` };

}

greetObject['Eric']; // => { message: 'Hello, Eric!' }

greetObject[]; // => null

1 đánh giá thành

javascript

function greetObject[who] {

if [!who] {

return null;

}

return { message: `Hello, ${who}!` };

}

greetObject['Eric']; // => { message: 'Hello, Eric!' }

greetObject[]; // => null

2.

2.1 null là giả mạo

// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
8, cùng với

javascript

function greetObject[who] {

if [!who] {

return null;

}

return { message: `Hello, ${who}!` };

}

greetObject['Eric']; // => { message: 'Hello, Eric!' }

greetObject[]; // => null

2,

javascript

function greetObject[who] {

if [!who] {

return null;

}

return { message: `Hello, ${who}!` };

}

greetObject['Eric']; // => { message: 'Hello, Eric!' }

greetObject[]; // => null

5,

javascript

function greetObject[who] {

if [!who] {

return null;

}

return { message: `Hello, ${who}!` };

}

greetObject['Eric']; // => { message: 'Hello, Eric!' }

greetObject[]; // => null

6,
typeof null          // "object" [not "null" for legacy reasons]
typeof undefined     // "undefined"
null === undefined   // false
null  == undefined   // true
null === null        // true
null  == null        // true
!null                // true
isNaN[1 + null]      // false
isNaN[1 + undefined] // true
2,

javascript

function greetObject[who] {

if [!who] {

return null;

}

return { message: `Hello, ${who}!` };

}

greetObject['Eric']; // => { message: 'Hello, Eric!' }

greetObject[]; // => null

8, là một giá trị giả. Nếu một giá trị giả xảy ra trong các điều kiện, thì JavaScript ép buộc giả với

javascript

function greetObject[who] {

if [!who] {

return null;

}

return { message: `Hello, ${who}!` };

}

greetObject['Eric']; // => { message: 'Hello, Eric!' }

greetObject[]; // => null

2.

javascript

Boolean[null]; // => false

if [null] {

console.log['null is truthy'];

} else {

console.log['null is falsy']; // logs 'null is falsy'

}

2.2 Loại null

Toán tử

javascript

const missingObject = null;

const existingObject = { message: 'Hello!' };

missingObject === null; // => true

existingObject === null; // => false

0 xác định loại giá trị. Ví dụ

javascript

const missingObject = null;

const existingObject = { message: 'Hello!' };

missingObject === null; // => true

existingObject === null; // => false

1 là

javascript

const missingObject = null;

const existingObject = { message: 'Hello!' };

missingObject === null; // => true

existingObject === null; // => false

2 và

javascript

const missingObject = null;

const existingObject = { message: 'Hello!' };

missingObject === null; // => true

existingObject === null; // => false

3 đánh giá thành

javascript

const missingObject = null;

const existingObject = { message: 'Hello!' };

missingObject === null; // => true

existingObject === null; // => false

4.

Thật thú vị, với giá trị nào

javascript

const missingObject = null;

const existingObject = { message: 'Hello!' };

missingObject === null; // => true

existingObject === null; // => false

5 đánh giá?

javascript

typeof null; // => 'object'

HM ... Làm thế nào loại đối tượng bị thiếu có thể đánh giá thành

javascript

const missingObject = null;

const existingObject = { message: 'Hello!' };

missingObject === null; // => true

existingObject === null; // => false

4? Hóa ra

javascript

const missingObject = null;

const existingObject = { message: 'Hello!' };

missingObject === null; // => true

existingObject === null; // => false

7 là

javascript

const missingObject = null;

const existingObject = { message: 'Hello!' };

missingObject === null; // => true

existingObject === null; // => false

4 là một sai lầm trong việc triển khai JavaScript ban đầu.

Không sử dụng toán tử

javascript

const missingObject = null;

const existingObject = { message: 'Hello!' };

missingObject === null; // => true

existingObject === null; // => false

9 để phát hiện giá trị
// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
8. Như đã đề cập trước đây, sử dụng toán tử bình đẳng nghiêm ngặt

javascript

Boolean[null]; // => false

if [null] {

console.log['null is truthy'];

} else {

console.log['null is falsy']; // logs 'null is falsy'

}

1.

Nếu bạn muốn kiểm tra xem một biến có chứa một đối tượng sử dụng toán tử

javascript

const missingObject = null;

const existingObject = { message: 'Hello!' };

missingObject === null; // => true

existingObject === null; // => false

9 hay không, bạn cũng phải kiểm tra lại
// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
8:

javascript

function isObject[object] {

return typeof object === 'object' && object !== null;

}

isObject[{ prop: 'Value' }]; // => true

isObject[15]; // => false

isObject[null]; // => false

3. cái bẫy của null

// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
8 có thể xuất hiện, thường bất ngờ, trong các tình huống khi bạn mong đợi một đối tượng. Sau đó, nếu bạn cố gắng trích xuất một thuộc tính từ
// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
8, JavaScript đã ném lỗi.

Chúng ta hãy sử dụng lại chức năng

javascript

let myObject = {

name: 'Eric Cartman'

};

7 và cố gắng truy cập thuộc tính

javascript

Boolean[null]; // => false

if [null] {

console.log['null is truthy'];

} else {

console.log['null is falsy']; // logs 'null is falsy'

}

7 từ đối tượng được trả về:

// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
0

Vì biến

javascript

let myObject = null;

4 là một chuỗi trống, hàm trả về
// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
8. Khi truy cập thuộc tính ____777 từ
// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
8, một lỗi kiểu loại được ném.

Bạn có thể xử lý

// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
8 bằng cách sử dụng chuỗi tùy chọn với sự kết hợp vô giá trị:

// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
1

hoặc sử dụng 2 lựa chọn thay thế được mô tả trong phần tiếp theo.

4. Các lựa chọn thay thế cho null

Thật hấp dẫn khi trả lại

// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
8 khi bạn không thể xây dựng một đối tượng. Nhưng thực hành này có nhược điểm.

Ngay khi

// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
8 xuất hiện trong ngăn xếp thực thi của bạn, bạn luôn phải kiểm tra nó.

Tôi cố gắng tránh trả lại

// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
8 ủng hộ:

  • trả về một đối tượng mặc định thay vì
    // foo is known to exist now but it has no type or value:
    const foo = null;
    foo; //null
    
    8
  • ném lỗi thay vì trả lại
    // foo is known to exist now but it has no type or value:
    const foo = null;
    foo; //null
    
    8

Hãy nhớ lại chức năng

javascript

let myObject = {

name: 'Eric Cartman'

};

7 trả về các đối tượng chào hỏi.

Thay vì trả về

// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
8 khi thiếu đối số, bạn có thể trả về một đối tượng mặc định:

// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
2

Hoặc là ném một lỗi:

// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
3

Những thực hành này cho phép bạn tránh đối phó với

// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
8.

5. null vs không xác định

typeof null          // "object" [not "null" for legacy reasons]
typeof undefined     // "undefined"
null === undefined   // false
null  == undefined   // true
null === null        // true
null  == null        // true
!null                // true
isNaN[1 + null]      // false
isNaN[1 + undefined] // true
2 là giá trị của một biến không được chỉ định hoặc thuộc tính đối tượng.

Ví dụ: nếu bạn khai báo một biến mà không gán giá trị ban đầu, việc truy cập biến đó sẽ đánh giá thành

typeof null          // "object" [not "null" for legacy reasons]
typeof undefined     // "undefined"
null === undefined   // false
null  == undefined   // true
null === null        // true
null  == null        // true
!null                // true
isNaN[1 + null]      // false
isNaN[1 + undefined] // true
2:

// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
4

Sự khác biệt chính giữa

// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
8 và
typeof null          // "object" [not "null" for legacy reasons]
typeof undefined     // "undefined"
null === undefined   // false
null  == undefined   // true
null === null        // true
null  == null        // true
!null                // true
isNaN[1 + null]      // false
isNaN[1 + undefined] // true
2 là
// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
8 đại diện cho một đối tượng bị thiếu, trong khi
typeof null          // "object" [not "null" for legacy reasons]
typeof undefined     // "undefined"
null === undefined   // false
null  == undefined   // true
null === null        // true
null  == null        // true
!null                // true
isNaN[1 + null]      // false
isNaN[1 + undefined] // true
2 đại diện cho một trạng thái không chính xác.

Toán tử bình đẳng nghiêm ngặt

javascript

function isObject[object] {

return typeof object === 'object' && object !== null;

}

isObject[{ prop: 'Value' }]; // => true

isObject[15]; // => false

isObject[null]; // => false

7 phân biệt
// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
8 với
typeof null          // "object" [not "null" for legacy reasons]
typeof undefined     // "undefined"
null === undefined   // false
null  == undefined   // true
null === null        // true
null  == null        // true
!null                // true
isNaN[1 + null]      // false
isNaN[1 + undefined] // true
2:

// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
5

Trong khi toán tử bình đẳng lỏng lẻo

// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
00 xem xét
// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
8 và
typeof null          // "object" [not "null" for legacy reasons]
typeof undefined     // "undefined"
null === undefined   // false
null  == undefined   // true
null === null        // true
null  == null        // true
!null                // true
isNaN[1 + null]      // false
isNaN[1 + undefined] // true
2 bằng nhau:

// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
6

Tôi sử dụng toán tử bình đẳng lỏng lẻo để kiểm tra xem một biến là

// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
8 hay
typeof null          // "object" [not "null" for legacy reasons]
typeof undefined     // "undefined"
null === undefined   // false
null  == undefined   // true
null === null        // true
null  == null        // true
!null                // true
isNaN[1 + null]      // false
isNaN[1 + undefined] // true
2:

// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
7

6. Tóm tắt

// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
8 là một giá trị đặc biệt trong JavaScript đại diện cho một đối tượng bị thiếu.

Toán tử bình đẳng nghiêm ngặt xác định xem một biến là NULL:

// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
06.

Toán tử

// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
07 rất hữu ích để xác định loại biến [số, chuỗi, boolean]. Tuy nhiên,

javascript

const missingObject = null;

const existingObject = { message: 'Hello!' };

missingObject === null; // => true

existingObject === null; // => false

9 là sai lệch trong trường hợp
// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
8:
// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
10 đánh giá thành

javascript

const missingObject = null;

const existingObject = { message: 'Hello!' };

missingObject === null; // => true

existingObject === null; // => false

4.

// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
8 và
typeof null          // "object" [not "null" for legacy reasons]
typeof undefined     // "undefined"
null === undefined   // false
null  == undefined   // true
null === null        // true
null  == null        // true
!null                // true
isNaN[1 + null]      // false
isNaN[1 + undefined] // true
2 bằng cách nào đó tương đương, vẫn còn,
// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
8 đại diện cho một đối tượng bị thiếu, trong khi
typeof null          // "object" [not "null" for legacy reasons]
typeof undefined     // "undefined"
null === undefined   // false
null  == undefined   // true
null === null        // true
null  == null        // true
!null                // true
isNaN[1 + null]      // false
isNaN[1 + undefined] // true
2 trạng thái không chính xác.

Tránh nếu có thể trả lại

// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
8 hoặc cài đặt các biến thành
// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
8. Thực tiễn này dẫn đến sự lây lan của các giá trị null và xác minh cho
// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
8. Thay vào đó, hãy cố gắng sử dụng các đối tượng với các thuộc tính mặc định hoặc thậm chí ném lỗi.

Đã thành thạo

// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
8, tại sao không thành thạo
typeof null          // "object" [not "null" for legacy reasons]
typeof undefined     // "undefined"
null === undefined   // false
null  == undefined   // true
null === null        // true
null  == null        // true
!null                // true
isNaN[1 + null]      // false
isNaN[1 + undefined] // true
2? Thực hiện theo bài viết 7 lời khuyên của tôi để xử lý không xác định trong JavaScript.

Bạn sử dụng điều kiện nào để kiểm tra

// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
8?

Null hay không phải là một javascript đối tượng?

Vì NULL không phải là một đối tượng trong JavaScript, sử dụng giá trị null khi một đối tượng được mong đợi không hoạt động.Một đối tượng thích hợp phải được cung cấp trong các tình huống như vậy.null is not an object in JavaScript, using a null value when an object is expected does not work. A proper object must be provided in such situations.

Nó có nghĩa là gì nếu một biến đối tượng là null?

Giá trị null đơn giản có nghĩa là biến không đề cập đến một đối tượng trong bộ nhớ.the variable does not refer to an object in memory.

Null có giống như đối tượng trống không?

Giá trị null đại diện cho sự vắng mặt của bất kỳ đối tượng nào, trong khi chuỗi trống là một đối tượng của chuỗi loại có ký tự bằng không.Nếu bạn cố gắng so sánh cả hai, chúng không giống nhau.they are not the same.

Chủ Đề