Để kiểm tra xem một đối tượng không thực sự có thuộc tính như vậy hay không và do đó sẽ trả về
var users = [23, 23, 23, 23, 23, 23, 23, 23, 23, 23];
console.log[Check[users, 0, 20]];
function Check[ids, counter, limit]{
ids.push[23];
// Recursion
if [counter+1 < limit]{
return Check[ids, counter+1, limit]; // return here!
}
else {
console.log[ids];
return ids;
}
}
1 theo mặc định khi bạn thử và truy cập nóif[!o.hasOwnProperty['myProperty']] {
alert["myProperty does not exist"];
}
Để kiểm tra xem giá trị được liên kết với mã định danh có phải là giá trị đặc biệt
var users = [23, 23, 23, 23, 23, 23, 23, 23, 23, 23];
console.log[Check[users, 0, 20]];
function Check[ids, counter, limit]{
ids.push[23];
// Recursion
if [counter+1 < limit]{
return Check[ids, counter+1, limit]; // return here!
}
else {
console.log[ids];
return ids;
}
}
1 hay mã định danh đó chưa được khai báo. Ghi chú. phương pháp này là cách duy nhất để đề cập đến một không được khai báo [lưu ý. khác với việc có giá trị của var users = [23, 23, 23, 23, 23, 23, 23, 23, 23, 23];
console.log[Check[users, 0, 20]];
function Check[ids, counter, limit]{
ids.push[23];
// Recursion
if [counter+1 < limit]{
return Check[ids, counter+1, limit]; // return here!
}
else {
console.log[ids];
return ids;
}
}
1] định danh mà không có lỗi sớmif[typeof myVariable === 'undefined'] {
alert['myVariable is either the special value `undefined`, or it has not been declared'];
}
Trong các phiên bản JavaScript trước ECMAScript 5, thuộc tính có tên "không xác định" trên đối tượng chung có thể ghi được và do đó, một kiểm tra đơn giản
var users = [23, 23, 23, 23, 23, 23, 23, 23, 23, 23];
console.log[Check[users, 0, 20]];
function Check[ids, counter, limit]{
ids.push[23];
// Recursion
if [counter+1 < limit]{
return Check[ids, counter+1, limit]; // return here!
}
else {
console.log[ids];
return ids;
}
}
3 có thể hoạt động không mong muốn nếu nó vô tình được xác định lại. Trong JavaScript hiện đại, thuộc tính chỉ đọcTuy nhiên, trong JavaScript hiện đại, "không xác định" không phải là một từ khóa và do đó, các biến bên trong các hàm có thể được đặt tên là "không xác định" và che khuất thuộc tính toàn cầu
Nếu bạn lo lắng về trường hợp cạnh [không chắc] này, bạn có thể sử dụng toán tử void để lấy giá trị đặc biệt
var users = [23, 23, 23, 23, 23, 23, 23, 23, 23, 23];
console.log[Check[users, 0, 20]];
function Check[ids, counter, limit]{
ids.push[23];
// Recursion
if [counter+1 < limit]{
return Check[ids, counter+1, limit]; // return here!
}
else {
console.log[ids];
return ids;
}
}
1if[myVariable === void 0] {
alert["myVariable is the special value `undefined`"];
}
Javascript – Tôi nên sử dụng giá trị “href” nào cho các liên kết JavaScript, “#” hoặc “javascript. khoảng trống [0]”
Tôi sử dụng
var users = [23, 23, 23, 23, 23, 23, 23, 23, 23, 23];
console.log[Check[users, 0, 20]];
function Check[ids, counter, limit]{
ids.push[23];
// Recursion
if [counter+1 < limit]{
return Check[ids, counter+1, limit]; // return here!
}
else {
console.log[ids];
return ids;
}
}
5Ba lý do. Việc khuyến khích sử dụng
var users = [23, 23, 23, 23, 23, 23, 23, 23, 23, 23];
console.log[Check[users, 0, 20]];
function Check[ids, counter, limit]{
ids.push[23];
// Recursion
if [counter+1 < limit]{
return Check[ids, counter+1, limit]; // return here!
}
else {
console.log[ids];
return ids;
}
}
6 trong nhóm các nhà phát triển chắc chắn sẽ dẫn đến việc một số người sử dụng giá trị trả về của hàm được gọi như thế nàyfunction doSomething[] {
//Some code
return false;
}
Nhưng sau đó họ quên sử dụng
var users = [23, 23, 23, 23, 23, 23, 23, 23, 23, 23];
console.log[Check[users, 0, 20]];
function Check[ids, counter, limit]{
ids.push[23];
// Recursion
if [counter+1 < limit]{
return Check[ids, counter+1, limit]; // return here!
}
else {
console.log[ids];
return ids;
}
}
7 trong title và chỉ sử dụng var users = [23, 23, 23, 23, 23, 23, 23, 23, 23, 23];
console.log[Check[users, 0, 20]];
function Check[ids, counter, limit]{
ids.push[23];
// Recursion
if [counter+1 < limit]{
return Check[ids, counter+1, limit]; // return here!
}
else {
console.log[ids];
return ids;
}
}
8Lý do thứ hai để tránh
var users = [23, 23, 23, 23, 23, 23, 23, 23, 23, 23];
console.log[Check[users, 0, 20]];
function Check[ids, counter, limit]{
ids.push[23];
// Recursion
if [counter+1 < limit]{
return Check[ids, counter+1, limit]; // return here!
}
else {
console.log[ids];
return ids;
}
}
6 là if[typeof myVariable === 'undefined'] {
alert['myVariable is either the special value `undefined`, or it has not been declared'];
}
00 cuối cùng sẽ không thực thi nếu hàm được gọi gây ra lỗi. Do đó, các nhà phát triển cũng phải nhớ xử lý bất kỳ lỗi nào một cách thích hợp trong hàm được gọiLý do thứ ba là có những trường hợp thuộc tính sự kiện
if[typeof myVariable === 'undefined'] {
alert['myVariable is either the special value `undefined`, or it has not been declared'];
}
01 được gán động. Tôi muốn có thể gọi một hàm hoặc gán nó một cách linh hoạt mà không cần phải viết mã hàm cụ thể cho phương thức đính kèm này hay phương thức khác. Do đó, if[typeof myVariable === 'undefined'] {
alert['myVariable is either the special value `undefined`, or it has not been declared'];
}
01 của tôi [hoặc trên bất kỳ thứ gì] trong phần đánh dấu HTML trông như thế nàyif[typeof myVariable === 'undefined'] {
alert['myVariable is either the special value `undefined`, or it has not been declared'];
}
3HOẶC
if[typeof myVariable === 'undefined'] {
alert['myVariable is either the special value `undefined`, or it has not been declared'];
}
4Sử dụng
var users = [23, 23, 23, 23, 23, 23, 23, 23, 23, 23];
console.log[Check[users, 0, 20]];
function Check[ids, counter, limit]{
ids.push[23];
// Recursion
if [counter+1 < limit]{
return Check[ids, counter+1, limit]; // return here!
}
else {
console.log[ids];
return ids;
}
}
5 sẽ tránh được tất cả các vấn đề đau đầu ở trên và tôi chưa tìm thấy bất kỳ ví dụ nào về nhược điểmVì vậy, nếu bạn là một nhà phát triển đơn độc thì rõ ràng bạn có thể đưa ra lựa chọn của riêng mình, nhưng nếu bạn làm việc theo nhóm, bạn phải
Sử dụng
if[typeof myVariable === 'undefined'] {
alert['myVariable is either the special value `undefined`, or it has not been declared'];
}
04, đảm bảo rằng if[typeof myVariable === 'undefined'] {
alert['myVariable is either the special value `undefined`, or it has not been declared'];
}
01 luôn chứa if[typeof myVariable === 'undefined'] {
alert['myVariable is either the special value `undefined`, or it has not been declared'];
}
00 ở cuối, rằng bất kỳ hàm nào được gọi sẽ không đưa ra lỗi và nếu bạn đính kèm một hàm động vào thuộc tính if[typeof myVariable === 'undefined'] {
alert['myVariable is either the special value `undefined`, or it has not been declared'];
}
01, hãy đảm bảo rằng cũng như không đưa ra lỗi, nó sẽ trả về if[typeof myVariable === 'undefined'] {
alert['myVariable is either the special value `undefined`, or it has not been declared'];
}
08