Hướng dẫn is null or not an object javascript? - là null hay không phải là một đối tượng javascript?

Giá trị // foo is known to exist now but it has no type or value: const foo = null; foo; //null 0 đạ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 0 được viết bằng chữ: // foo is known to exist now but it has no type or value: const foo = null; foo; //null 0. // foo is known to exist now but it has no type or value: const foo = null; foo; //null 0 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ư // foo is known to exist now but it has no type or value: const foo = null; foo; //null 4 có thể. Thay vào đó, // foo is known to exist now but it has no type or value: const foo = null; foo; //null 0 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 0 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 0 và // foo is known to exist now but it has no type or value: const foo = null; foo; //null 4

Khi kiểm tra // foo is known to exist now but it has no type or value: const foo = null; foo; //null 0 hoặc // foo is known to exist now but it has no type or value: const foo = null; foo; //null 4, 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

Tôi đang gặp lỗi trong thanh tra web như được hiển thị bên dưới:

TypeError: 'null' is not an object (evaluating 'myButton. title = function() { var userName = myTextfield.value; greetUser(userName); return false; }')

Đây là mã của tôi (HTML):

<h2>Hello World!</h2> <p id="myParagraph">This is an example website</p> <script src="js/script.js" type="text/javascript"></script> <form> <input type="text" id="myTextfield" placeholder="Type your name" /> <input type="submit" id="myButton" value="Go" /> </form>

Đây là JS:

var myButton = document.getElementById("myButton"); var myTextfield = document.getElementById("myTextfield"); function greetUser(userName) { var greeting = "Hello " + userName + "!"; document.getElementsByTagName ("h2")[0].innerHTML = greeting; } myButton. title = function() { var userName = myTextfield.value; greetUser(userName); return false; }

Bất kỳ ý tưởng tại sao tôi nhận được lỗi?

Hỏi ngày 8 tháng 1 năm 2013 lúc 3:48Jan 8, 2013 at 3:48

1

Đặt mã để nó thực thi sau khi các phần tử được xác định, với một cuộc gọi lại sẵn sàng DOM hoặc đặt nguồn theo các phần tử trong HTML.

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 1 trả về // foo is known to exist now but it has no type or value: const foo = null; foo; //null 0 nếu phần tử không thể được tìm thấy. Việc gán tài sản chỉ có thể xảy ra trên các đối tượng. // foo is known to exist now but it has no type or value: const foo = null; foo; //null 0 không phải là một đối tượng (trái với những gì 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 4 nói).

Đã trả lời ngày 8 tháng 1 năm 2013 lúc 3:49Jan 8, 2013 at 3:49

Alexalexalex

468K197 Huy hiệu vàng867 Huy hiệu bạc975 Huy hiệu Đồng197 gold badges867 silver badges975 bronze badges

4

Bất kỳ mã JS nào thực thi và xử lý các thành phần DOM sẽ thực thi sau khi các thành phần DOM đã được tạo. Mã JS được giải thích từ trên xuống dưới như được đặt trong HTML. Vì vậy, nếu có một thẻ trước các thành phần DOM, mã JS trong thẻ tập lệnh sẽ thực thi khi trình duyệt phân tích trang HTML.

Vì vậy, trong trường hợp của bạn, bạn có thể đặt mã tương tác DOM của mình vào trong một hàm để chỉ xác định hàm nhưng không được thực thi.

Sau đó, bạn có thể thêm trình nghe sự kiện để tải tài liệu để thực thi chức năng.

Điều đó sẽ cung cấp cho bạn một cái gì đó như:

<script> function init() { var myButton = document.getElementById("myButton"); var myTextfield = document.getElementById("myTextfield"); myButton. title = function() { var userName = myTextfield.value; greetUser(userName); } } function greetUser(userName) { var greeting = "Hello " + userName + "!"; document.getElementsByTagName ("h2")[0].innerHTML = greeting; } document.addEventListener('readystatechange', function() { if (document.readyState === "complete") { init(); } }); </script> <h2>Hello World!</h2> <p id="myParagraph">This is an example website</p> <form> <input type="text" id="myTextfield" placeholder="Type your name" /> <input type="button" id="myButton" value="Go" /> </form>

Fiddle tại - //jsfiddle.net/poonia/qqmeg/4/

Đã trả lời ngày 8 tháng 1 năm 2013 lúc 4:20Jan 8, 2013 at 4:20

Tôi nghĩ rằng lỗi vì các yếu tố không được xác định, vì vậy bạn cần thêm sự kiện 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 5 mà sự kiện này sẽ xác định các yếu tố của bạn khi cửa sổ được tải.

window.addEventListener('load',Loaded,false); function Loaded(){ var myButton = document.getElementById("myButton"); var myTextfield = document.getElementById("myTextfield"); function greetUser(userName) { var greeting = "Hello " + userName + "!"; document.getElementsByTagName ("h2")[0].innerHTML = greeting; } myButton. title = function() { var userName = myTextfield.value; greetUser(userName); return false; } }

Đã trả lời ngày 8 tháng 1 năm 2013 lúc 4:31Jan 8, 2013 at 4:31

Hãy thử tải JavaScript của bạn sau.

Thử cái này:

<h2>Hello World!</h2> <p id="myParagraph">This is an example website</p> <form> <input type="text" id="myTextfield" placeholder="Type your name" /> <input type="submit" id="myButton" value="Go" /> </form> <script src="js/script.js" type="text/javascript"></script>

Đã trả lời ngày 8 tháng 1 năm 2013 lúc 3:56Jan 8, 2013 at 3:56

Bryan CSBRYAN CSBryan CS

5815 Huy hiệu bạc19 Huy hiệu đồng5 silver badges19 bronze badges

Tôi đồng ý với Alex về việc đảm bảo DOM được tải. Tôi cũng nghĩ rằng nút gửi sẽ kích hoạt làm mới.

Đây là những gì tôi sẽ làm

<html> <head> <title>webpage</title> </head> <script type="text/javascript"> var myButton; var myTextfield; function setup() { myButton = document.getElementById("myButton"); myTextfield = document.getElementById("myTextfield"); myButton. title = function() { var userName = myTextfield.value; greetUser(userName); return false; } } function greetUser(userName) { var greeting = "Hello " + userName + "!"; document.getElementsByTagName("h2")[0].innerHTML = greeting; } </script> <body onload="setup()"> <h2>Hello World!</h2> <p id="myParagraph">This is an example website</p> <form> <input type="text" id="myTextfield" placeholder="Type your name" /> <input type="button" id="myButton" value="Go" /> </form> </body> </html>

chúc vui vẻ!

Đã trả lời ngày 8 tháng 1 năm 2013 lúc 4:07Jan 8, 2013 at 4:07

EarlonRailSearLonRailsearlonrails

4.8063 Huy hiệu vàng31 Huy hiệu bạc47 Huy hiệu đồng3 gold badges31 silver badges47 bronze badges

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

Trong JavaScript null là "không có gì".Nó được cho là một cái gì đó không tồn tại.Thật không may, trong JavaScript, loại dữ liệu của NULL là một đối tượng.Bạn có thể coi đó là một lỗi trong JavaScript rằng typeof null là một đối tượng.in JavaScript, the data type of null is an object. You can consider it a bug in JavaScript that typeof null is an object.

Là đối tượng null hay chuỗi?

Trong Java, có một sự khác biệt rõ rệt giữa các chuỗi null, trống và trống.Một chuỗi trống là một đối tượng chuỗi có giá trị được gán, nhưng độ dài của nó bằng 0.Một chuỗi null không có giá trị nào cả.A null string has no value at all.

Làm thế nào để bạn kiểm tra xem một đối tượng có trống hay không trong JavaScript?

Sử dụng đối tượng.Sự vật.Các phím sẽ trả về một mảng, chứa tên thuộc tính của đối tượng.Nếu độ dài của mảng là 0, thì chúng ta biết rằng đối tượng trống. Object. keys will return an array, which contains the property names of the object. If the length of the array is 0 , then we know that the object is empty.

Không phải là loại đối tượng JavaScript?

Một dữ liệu không phải là một đối tượng và không có bất kỳ phương thức nào.JavaScript có 7 loại dữ liệu nguyên thủy: chuỗi, số, bigint, boolean, không xác định, biểu tượng và null.string, number, bigint, boolean, undefined, symbol, and null.

Chủ đề