Làm mới phần tử DOM javascript

Tải lại có thể bị chặn và ném một

function foo[] {
  const secret = Math.trunc[Math.random[]*100]
  return function inner[] {
    console.log[`The secret number is ${secret}.`]
  }
}
const f = foo[] // `secret` is not directly accessible from outside `foo`
f[] // The only way to retrieve `secret`, is to invoke `f`
6
function foo[] {
  const secret = Math.trunc[Math.random[]*100]
  return function inner[] {
    console.log[`The secret number is ${secret}.`]
  }
}
const f = foo[] // `secret` is not directly accessible from outside `foo`
f[] // The only way to retrieve `secret`, is to invoke `f`
7. Điều này xảy ra nếu nguồn gốc của tập lệnh gọi
function foo[] {
  const secret = Math.trunc[Math.random[]*100]
  return function inner[] {
    console.log[`The secret number is ${secret}.`]
  }
}
const f = foo[] // `secret` is not directly accessible from outside `foo`
f[] // The only way to retrieve `secret`, is to invoke `f`
8 khác với nguồn gốc của trang sở hữu đối tượng
function foo[] {
  const secret = Math.trunc[Math.random[]*100]
  return function inner[] {
    console.log[`The secret number is ${secret}.`]
  }
}
const f = foo[] // `secret` is not directly accessible from outside `foo`
f[] // The only way to retrieve `secret`, is to invoke `f`
9. Xem Chính sách cùng nguồn gốc để biết thêm thông tin

cú pháp

reload[]

Thông số

Không có

Ghi chú. Firefox hỗ trợ tham số boolean

function foo[] {
  const secret = Math.trunc[Math.random[]*100]
  return function inner[] {
    console.log[`The secret number is ${secret}.`]
  }
}
const f = foo[] // `secret` is not directly accessible from outside `foo`
f[] // The only way to retrieve `secret`, is to invoke `f`
0 không chuẩn cho
function foo[] {
  const secret = Math.trunc[Math.random[]*100]
  return function inner[] {
    console.log[`The secret number is ${secret}.`]
  }
}
const f = foo[] // `secret` is not directly accessible from outside `foo`
f[] // The only way to retrieve `secret`, is to invoke `f`
8, để yêu cầu Firefox bỏ qua bộ nhớ đệm và buộc tải lại tài liệu hiện tại. Tuy nhiên, trong tất cả các trình duyệt khác, bất kỳ tham số nào bạn chỉ định trong lệnh gọi
function foo[] {
  const secret = Math.trunc[Math.random[]*100]
  return function inner[] {
    console.log[`The secret number is ${secret}.`]
  }
}
const f = foo[] // `secret` is not directly accessible from outside `foo`
f[] // The only way to retrieve `secret`, is to invoke `f`
8 sẽ bị bỏ qua và không có bất kỳ ảnh hưởng nào

Tuy nhiên, bạn có thể bắt gặp các trường hợp của

function foo[] {
  const secret = Math.trunc[Math.random[]*100]
  return function inner[] {
    console.log[`The secret number is ${secret}.`]
  }
}
const f = foo[] // `secret` is not directly accessible from outside `foo`
f[] // The only way to retrieve `secret`, is to invoke `f`
3 trong mã hiện có được viết với giả định rằng hiệu ứng buộc tải lại xảy ra trong tất cả các trình duyệt. Tìm kiếm GitHub "
function foo[] {
  const secret = Math.trunc[Math.random[]*100]
  return function inner[] {
    console.log[`The secret number is ${secret}.`]
  }
}
const f = foo[] // `secret` is not directly accessible from outside `foo`
f[] // The only way to retrieve `secret`, is to invoke `f`
3" trả về hàng trăm nghìn kết quả. Vì vậy, có rất nhiều mã hiện có

Lịch sử của nó là. một số phiên bản của Netscape Navigator đã thêm hỗ trợ cho nó, mà dường như cuối cùng đã được chọn trong Firefox. Và tại một thời điểm, Nhóm làm việc về API web của W3C đã đưa ra một vấn đề để xem xét thêm nó vào thông số kỹ thuật cho

function foo[] {
  const secret = Math.trunc[Math.random[]*100]
  return function inner[] {
    console.log[`The secret number is ${secret}.`]
  }
}
const f = foo[] // `secret` is not directly accessible from outside `foo`
f[] // The only way to retrieve `secret`, is to invoke `f`
8. Tuy nhiên, nó chưa bao giờ thực sự được thêm vào

Vì vậy, một tham số boolean không phải là một phần của thông số kỹ thuật hiện tại cho

function foo[] {
  const secret = Math.trunc[Math.random[]*100]
  return function inner[] {
    console.log[`The secret number is ${secret}.`]
  }
}
const f = foo[] // `secret` is not directly accessible from outside `foo`
f[] // The only way to retrieve `secret`, is to invoke `f`
8 - và trên thực tế chưa bao giờ là một phần của bất kỳ thông số kỹ thuật nào cho
function foo[] {
  const secret = Math.trunc[Math.random[]*100]
  return function inner[] {
    console.log[`The secret number is ${secret}.`]
  }
}
const f = foo[] // `secret` is not directly accessible from outside `foo`
f[] // The only way to retrieve `secret`, is to invoke `f`
8 từng được xuất bản

Có những phần tử mà tôi có thể ghi lại một bước nhưng quá trình thực thi không thành công với một phần tử không tìm thấy ngoại lệ

Một số phần tử trong ứng dụng được tải động trong DOM trong quá trình thực thi - các ví dụ điển hình có thể là các mục menu thả xuống, mẹo công cụ, v.v. Điều này có thể khiến việc thực thi không thành công do các phần tử động không có trong cây DOM khi trang được tải lần đầu tiên. Chúng chỉ xuất hiện sau một hành động nhất định đối với ứng dụng [di chuột qua, nhấp vào trình đơn thả xuống, v.v. ]

Rất thường xuyên, trong tình huống như vậy, việc làm mới DOM sau hành động cụ thể đó sẽ cho phép quá trình thực thi diễn ra suôn sẻ. Không có bước giao diện người dùng nào cho điều đó mặc dù có thể dễ dàng lấy được trong một bước được mã hóa. Dòng mã duy nhất bạn cần thêm là như sau

Tôi có một phần tử DIV trống trong đó tôi nối thêm hình ảnh bằng cách sử dụng hàm createElement["img"] và nối chúng với appendChild. Vì vậy, bây giờ tôi đã có phần tử DIV đầy hình ảnh

Tôi muốn sử dụng một nút để xóa DIV này và đồng thời thêm hình ảnh mới vào đó.
Cảm ơn sự giúp đỡ của bạn

Giải pháp tốt nhất

Có phải bạn chỉ đang tìm kiếm phương pháp

function foo[] {
  const secret = Math.trunc[Math.random[]*100]
  return function inner[] {
    console.log[`The secret number is ${secret}.`]
  }
}
const f = foo[] // `secret` is not directly accessible from outside `foo`
f[] // The only way to retrieve `secret`, is to invoke `f`
1

// assuming yor div is in variable divelement
while [divelement.firstChild]
  divelement.removeChild[divelement.firstChild];

Giải pháp liên quan

Javascript – Cách đóng JavaScript hoạt động

Một đóng cửa là một cặp của

  1. Một chức năng, và
  2. Tham chiếu đến phạm vi bên ngoài của hàm đó [môi trường từ vựng]

Môi trường từ vựng là một phần của mọi ngữ cảnh thực thi [khung ngăn xếp] và là bản đồ giữa các mã định danh [nghĩa là. tên biến cục bộ] và giá trị

Mỗi chức năng trong JavaScript duy trì một tham chiếu đến môi trường từ vựng bên ngoài của nó. Tham chiếu này được sử dụng để định cấu hình ngữ cảnh thực thi được tạo khi một hàm được gọi. Tham chiếu này cho phép mã bên trong hàm "nhìn thấy" các biến được khai báo bên ngoài hàm, bất kể khi nào và ở đâu hàm được gọi

Nếu một chức năng được gọi bởi một chức năng, đến lượt nó được gọi bởi một chức năng khác, thì một chuỗi tham chiếu đến môi trường từ vựng bên ngoài được tạo. Chuỗi này được gọi là chuỗi phạm vi

Trong đoạn mã sau,

function foo[] {
  const secret = Math.trunc[Math.random[]*100]
  return function inner[] {
    console.log[`The secret number is ${secret}.`]
  }
}
const f = foo[] // `secret` is not directly accessible from outside `foo`
f[] // The only way to retrieve `secret`, is to invoke `f`
2 tạo thành một bao đóng với môi trường từ vựng của ngữ cảnh thực thi được tạo khi gọi
function foo[] {
  const secret = Math.trunc[Math.random[]*100]
  return function inner[] {
    console.log[`The secret number is ${secret}.`]
  }
}
const f = foo[] // `secret` is not directly accessible from outside `foo`
f[] // The only way to retrieve `secret`, is to invoke `f`
3, đóng trên biến
function foo[] {
  const secret = Math.trunc[Math.random[]*100]
  return function inner[] {
    console.log[`The secret number is ${secret}.`]
  }
}
const f = foo[] // `secret` is not directly accessible from outside `foo`
f[] // The only way to retrieve `secret`, is to invoke `f`
4

function foo[] {
  const secret = Math.trunc[Math.random[]*100]
  return function inner[] {
    console.log[`The secret number is ${secret}.`]
  }
}
const f = foo[] // `secret` is not directly accessible from outside `foo`
f[] // The only way to retrieve `secret`, is to invoke `f`

Nói cách khác. trong JavaScript, các hàm mang tham chiếu đến một "hộp trạng thái" riêng tư mà chỉ chúng [và bất kỳ hàm nào khác được khai báo trong cùng một môi trường từ vựng] mới có quyền truy cập. Hộp trạng thái này là vô hình đối với người gọi hàm, cung cấp một cơ chế tuyệt vời để ẩn và đóng gói dữ liệu

Và hãy nhớ. các hàm trong JavaScript có thể được truyền xung quanh giống như các biến [hàm hạng nhất], nghĩa là các cặp chức năng và trạng thái này có thể được truyền xung quanh chương trình của bạn. tương tự như cách bạn có thể truyền một thể hiện của một lớp xung quanh trong C++

Nếu JavaScript không có bao đóng, thì sẽ phải chuyển nhiều trạng thái hơn giữa các hàm một cách rõ ràng, làm cho danh sách tham số dài hơn và mã ồn hơn

Vì vậy, nếu bạn muốn một chức năng luôn có quyền truy cập vào một phần trạng thái riêng tư, bạn có thể sử dụng bao đóng

và chúng ta thường muốn liên kết trạng thái với một chức năng. Ví dụ, trong Java hoặc C++, khi bạn thêm một biến thể hiện riêng và một phương thức vào một lớp, bạn đang liên kết trạng thái với chức năng

Trong C và hầu hết các ngôn ngữ phổ biến khác, sau khi một hàm trả về, tất cả các biến cục bộ không còn truy cập được nữa do khung ngăn xếp bị hủy. Trong JavaScript, nếu bạn khai báo một hàm bên trong một hàm khác, thì các biến cục bộ của hàm bên ngoài vẫn có thể truy cập được sau khi trở về từ hàm đó. Theo cách này, trong đoạn mã trên,

function foo[] {
  const secret = Math.trunc[Math.random[]*100]
  return function inner[] {
    console.log[`The secret number is ${secret}.`]
  }
}
const f = foo[] // `secret` is not directly accessible from outside `foo`
f[] // The only way to retrieve `secret`, is to invoke `f`
4 vẫn có sẵn cho đối tượng hàm
function foo[] {
  const secret = Math.trunc[Math.random[]*100]
  return function inner[] {
    console.log[`The secret number is ${secret}.`]
  }
}
const f = foo[] // `secret` is not directly accessible from outside `foo`
f[] // The only way to retrieve `secret`, is to invoke `f`
2, sau khi nó được trả về từ
function foo[] {
  const secret = Math.trunc[Math.random[]*100]
  return function inner[] {
    console.log[`The secret number is ${secret}.`]
  }
}
const f = foo[] // `secret` is not directly accessible from outside `foo`
f[] // The only way to retrieve `secret`, is to invoke `f`
3

Công dụng của Đóng cửa

Các bao đóng hữu ích bất cứ khi nào bạn cần một trạng thái riêng tư được liên kết với một chức năng. Đây là một kịch bản rất phổ biến - và hãy nhớ. JavaScript không có cú pháp lớp cho đến năm 2015 và nó vẫn không có cú pháp trường riêng. Đóng cửa đáp ứng nhu cầu này

Biến thể hiện cá nhân

Trong đoạn mã sau, hàm

function foo[] {
  const secret = Math.trunc[Math.random[]*100]
  return function inner[] {
    console.log[`The secret number is ${secret}.`]
  }
}
const f = foo[] // `secret` is not directly accessible from outside `foo`
f[] // The only way to retrieve `secret`, is to invoke `f`
8 đóng trên các chi tiết của ô tô

function foo[] {
  const secret = Math.trunc[Math.random[]*100]
  return function inner[] {
    console.log[`The secret number is ${secret}.`]
  }
}
const f = foo[] // `secret` is not directly accessible from outside `foo`
f[] // The only way to retrieve `secret`, is to invoke `f`
1

Lập trình chức năng

Trong đoạn mã sau, hàm

function foo[] {
  const secret = Math.trunc[Math.random[]*100]
  return function inner[] {
    console.log[`The secret number is ${secret}.`]
  }
}
const f = foo[] // `secret` is not directly accessible from outside `foo`
f[] // The only way to retrieve `secret`, is to invoke `f`
2 đóng trên cả
function foo[] {
  const secret = Math.trunc[Math.random[]*100]
  return function inner[] {
    console.log[`The secret number is ${secret}.`]
  }
}
const f = foo[] // `secret` is not directly accessible from outside `foo`
f[] // The only way to retrieve `secret`, is to invoke `f`
10 và
function foo[] {
  const secret = Math.trunc[Math.random[]*100]
  return function inner[] {
    console.log[`The secret number is ${secret}.`]
  }
}
const f = foo[] // `secret` is not directly accessible from outside `foo`
f[] // The only way to retrieve `secret`, is to invoke `f`
11

function foo[] {
  const secret = Math.trunc[Math.random[]*100]
  return function inner[] {
    console.log[`The secret number is ${secret}.`]
  }
}
const f = foo[] // `secret` is not directly accessible from outside `foo`
f[] // The only way to retrieve `secret`, is to invoke `f`
5

Lập trình hướng sự kiện

Trong đoạn mã sau, hàm

function foo[] {
  const secret = Math.trunc[Math.random[]*100]
  return function inner[] {
    console.log[`The secret number is ${secret}.`]
  }
}
const f = foo[] // `secret` is not directly accessible from outside `foo`
f[] // The only way to retrieve `secret`, is to invoke `f`
12 đóng trên biến
function foo[] {
  const secret = Math.trunc[Math.random[]*100]
  return function inner[] {
    console.log[`The secret number is ${secret}.`]
  }
}
const f = foo[] // `secret` is not directly accessible from outside `foo`
f[] // The only way to retrieve `secret`, is to invoke `f`
13

function foo[] {
  const secret = Math.trunc[Math.random[]*100]
  return function inner[] {
    console.log[`The secret number is ${secret}.`]
  }
}
const f = foo[] // `secret` is not directly accessible from outside `foo`
f[] // The only way to retrieve `secret`, is to invoke `f`
8
function foo[] {
  const secret = Math.trunc[Math.random[]*100]
  return function inner[] {
    console.log[`The secret number is ${secret}.`]
  }
}
const f = foo[] // `secret` is not directly accessible from outside `foo`
f[] // The only way to retrieve `secret`, is to invoke `f`
9

mô đun hóa

Trong ví dụ sau, tất cả các chi tiết triển khai được ẩn bên trong một biểu thức hàm được thực thi ngay lập tức. Các chức năng

function foo[] {
  const secret = Math.trunc[Math.random[]*100]
  return function inner[] {
    console.log[`The secret number is ${secret}.`]
  }
}
const f = foo[] // `secret` is not directly accessible from outside `foo`
f[] // The only way to retrieve `secret`, is to invoke `f`
14 và
function foo[] {
  const secret = Math.trunc[Math.random[]*100]
  return function inner[] {
    console.log[`The secret number is ${secret}.`]
  }
}
const f = foo[] // `secret` is not directly accessible from outside `foo`
f[] // The only way to retrieve `secret`, is to invoke `f`
8 đóng trên trạng thái tư nhân và các chức năng họ cần để hoàn thành công việc của mình. Việc đóng cửa đã cho phép chúng tôi mô đun hóa và đóng gói mã của chúng tôi

// assuming yor div is in variable divelement
while [divelement.firstChild]
  divelement.removeChild[divelement.firstChild];
2

ví dụ

ví dụ 1

Ví dụ này cho thấy các biến cục bộ không được sao chép trong bao đóng. việc đóng cửa duy trì một tham chiếu đến chính các biến ban đầu. Như thể khung ngăn xếp vẫn tồn tại trong bộ nhớ ngay cả sau khi chức năng bên ngoài thoát

// assuming yor div is in variable divelement
while [divelement.firstChild]
  divelement.removeChild[divelement.firstChild];
3

ví dụ 2

Trong đoạn mã sau, ba phương thức

function foo[] {
  const secret = Math.trunc[Math.random[]*100]
  return function inner[] {
    console.log[`The secret number is ${secret}.`]
  }
}
const f = foo[] // `secret` is not directly accessible from outside `foo`
f[] // The only way to retrieve `secret`, is to invoke `f`
16,
function foo[] {
  const secret = Math.trunc[Math.random[]*100]
  return function inner[] {
    console.log[`The secret number is ${secret}.`]
  }
}
const f = foo[] // `secret` is not directly accessible from outside `foo`
f[] // The only way to retrieve `secret`, is to invoke `f`
17 và
function foo[] {
  const secret = Math.trunc[Math.random[]*100]
  return function inner[] {
    console.log[`The secret number is ${secret}.`]
  }
}
const f = foo[] // `secret` is not directly accessible from outside `foo`
f[] // The only way to retrieve `secret`, is to invoke `f`
18 đều đóng trên cùng một môi trường từ vựng

Và mỗi khi

function foo[] {
  const secret = Math.trunc[Math.random[]*100]
  return function inner[] {
    console.log[`The secret number is ${secret}.`]
  }
}
const f = foo[] // `secret` is not directly accessible from outside `foo`
f[] // The only way to retrieve `secret`, is to invoke `f`
19 được gọi, một bối cảnh thực thi mới [khung ngăn xếp] được tạo và một biến hoàn toàn mới
function foo[] {
  const secret = Math.trunc[Math.random[]*100]
  return function inner[] {
    console.log[`The secret number is ${secret}.`]
  }
}
const f = foo[] // `secret` is not directly accessible from outside `foo`
f[] // The only way to retrieve `secret`, is to invoke `f`
50 và một bộ hàm mới [
function foo[] {
  const secret = Math.trunc[Math.random[]*100]
  return function inner[] {
    console.log[`The secret number is ${secret}.`]
  }
}
const f = foo[] // `secret` is not directly accessible from outside `foo`
f[] // The only way to retrieve `secret`, is to invoke `f`
16, v.v. ] được tạo, đóng trên biến mới này

function foo[] {
  const secret = Math.trunc[Math.random[]*100]
  return function inner[] {
    console.log[`The secret number is ${secret}.`]
  }
}
const f = foo[] // `secret` is not directly accessible from outside `foo`
f[] // The only way to retrieve `secret`, is to invoke `f`
0

ví dụ 3

Nếu bạn đang sử dụng các biến được khai báo bằng cách sử dụng

function foo[] {
  const secret = Math.trunc[Math.random[]*100]
  return function inner[] {
    console.log[`The secret number is ${secret}.`]
  }
}
const f = foo[] // `secret` is not directly accessible from outside `foo`
f[] // The only way to retrieve `secret`, is to invoke `f`
52, hãy cẩn thận để biết bạn đang đóng biến nào. Các biến được khai báo bằng cách sử dụng
function foo[] {
  const secret = Math.trunc[Math.random[]*100]
  return function inner[] {
    console.log[`The secret number is ${secret}.`]
  }
}
const f = foo[] // `secret` is not directly accessible from outside `foo`
f[] // The only way to retrieve `secret`, is to invoke `f`
52 được nâng lên. Đây là một vấn đề ít hơn nhiều trong JavaScript hiện đại do sự ra đời của
function foo[] {
  const secret = Math.trunc[Math.random[]*100]
  return function inner[] {
    console.log[`The secret number is ${secret}.`]
  }
}
const f = foo[] // `secret` is not directly accessible from outside `foo`
f[] // The only way to retrieve `secret`, is to invoke `f`
54 và
function foo[] {
  const secret = Math.trunc[Math.random[]*100]
  return function inner[] {
    console.log[`The secret number is ${secret}.`]
  }
}
const f = foo[] // `secret` is not directly accessible from outside `foo`
f[] // The only way to retrieve `secret`, is to invoke `f`
55

Trong đoạn mã sau, mỗi lần lặp lại, một hàm mới

function foo[] {
  const secret = Math.trunc[Math.random[]*100]
  return function inner[] {
    console.log[`The secret number is ${secret}.`]
  }
}
const f = foo[] // `secret` is not directly accessible from outside `foo`
f[] // The only way to retrieve `secret`, is to invoke `f`
2 được tạo, hàm này sẽ đóng trên
function foo[] {
  const secret = Math.trunc[Math.random[]*100]
  return function inner[] {
    console.log[`The secret number is ${secret}.`]
  }
}
const f = foo[] // `secret` is not directly accessible from outside `foo`
f[] // The only way to retrieve `secret`, is to invoke `f`
57. Nhưng vì
function foo[] {
  const secret = Math.trunc[Math.random[]*100]
  return function inner[] {
    console.log[`The secret number is ${secret}.`]
  }
}
const f = foo[] // `secret` is not directly accessible from outside `foo`
f[] // The only way to retrieve `secret`, is to invoke `f`
58 được nâng lên bên ngoài vòng lặp, tất cả các hàm bên trong này đóng trên cùng một biến, nghĩa là giá trị cuối cùng của
function foo[] {
  const secret = Math.trunc[Math.random[]*100]
  return function inner[] {
    console.log[`The secret number is ${secret}.`]
  }
}
const f = foo[] // `secret` is not directly accessible from outside `foo`
f[] // The only way to retrieve `secret`, is to invoke `f`
57 [3] được in ba lần

function foo[] {
  const secret = Math.trunc[Math.random[]*100]
  return function inner[] {
    console.log[`The secret number is ${secret}.`]
  }
}
const f = foo[] // `secret` is not directly accessible from outside `foo`
f[] // The only way to retrieve `secret`, is to invoke `f`
9

điểm cuối cùng

  • Bất cứ khi nào một hàm được khai báo trong JavaScript, bao đóng được tạo
  • Trả về một
    function foo[] {
      const secret = Math.trunc[Math.random[]*100]
      return function inner[] {
        console.log[`The secret number is ${secret}.`]
      }
    }
    const f = foo[] // `secret` is not directly accessible from outside `foo`
    f[] // The only way to retrieve `secret`, is to invoke `f`
    80 từ bên trong một hàm khác là ví dụ cổ điển về đóng, bởi vì trạng thái bên trong hàm bên ngoài hoàn toàn có sẵn cho hàm bên trong được trả về, ngay cả sau khi hàm bên ngoài đã hoàn tất thực thi
  • Bất cứ khi nào bạn sử dụng
    function foo[] {
      const secret = Math.trunc[Math.random[]*100]
      return function inner[] {
        console.log[`The secret number is ${secret}.`]
      }
    }
    const f = foo[] // `secret` is not directly accessible from outside `foo`
    f[] // The only way to retrieve `secret`, is to invoke `f`
    81 bên trong một hàm, thì bao đóng được sử dụng. Văn bản mà bạn
    function foo[] {
      const secret = Math.trunc[Math.random[]*100]
      return function inner[] {
        console.log[`The secret number is ${secret}.`]
      }
    }
    const f = foo[] // `secret` is not directly accessible from outside `foo`
    f[] // The only way to retrieve `secret`, is to invoke `f`
    82 có thể tham chiếu đến các biến cục bộ của hàm và ở chế độ không nghiêm ngặt, bạn thậm chí có thể tạo các biến cục bộ mới bằng cách sử dụng
    function foo[] {
      const secret = Math.trunc[Math.random[]*100]
      return function inner[] {
        console.log[`The secret number is ${secret}.`]
      }
    }
    const f = foo[] // `secret` is not directly accessible from outside `foo`
    f[] // The only way to retrieve `secret`, is to invoke `f`
    83
  • Khi bạn sử dụng
    function foo[] {
      const secret = Math.trunc[Math.random[]*100]
      return function inner[] {
        console.log[`The secret number is ${secret}.`]
      }
    }
    const f = foo[] // `secret` is not directly accessible from outside `foo`
    f[] // The only way to retrieve `secret`, is to invoke `f`
    84 [Hàm tạo chức năng] bên trong một chức năng, nó sẽ không đóng trên môi trường từ vựng của nó. thay vào đó, nó đóng trên bối cảnh toàn cầu. Hàm mới không thể tham chiếu các biến cục bộ của hàm bên ngoài
  • Một bao đóng trong JavaScript giống như giữ một tham chiếu [KHÔNG phải là một bản sao] đến phạm vi tại điểm khai báo hàm, từ đó giữ một tham chiếu đến phạm vi bên ngoài của nó, v.v., đến tận đối tượng chung ở đầu
  • Một bao đóng được tạo khi một hàm được khai báo;
  • Một tập hợp các biến cục bộ mới được tạo mỗi khi một hàm được gọi

liên kết

  • Các thuộc tính riêng tư mô phỏng của Douglas Crockford và các phương thức riêng tư cho một đối tượng, sử dụng các bao đóng
  • Một lời giải thích tuyệt vời về cách đóng cửa có thể gây rò rỉ bộ nhớ trong IE nếu bạn không cẩn thận
  • Tài liệu MDN về Đóng JavaScript

Javascript – Cách kiểm tra xem một phần tử có bị ẩn trong jQuery không

Vì câu hỏi đề cập đến một yếu tố duy nhất, nên mã này có thể phù hợp hơn

function foo[] {
  const secret = Math.trunc[Math.random[]*100]
  return function inner[] {
    console.log[`The secret number is ${secret}.`]
  }
}
const f = foo[] // `secret` is not directly accessible from outside `foo`
f[] // The only way to retrieve `secret`, is to invoke `f`
0

Nó giống như đề xuất của twernt, nhưng được áp dụng cho một phần tử duy nhất;

Chúng tôi sử dụng hàm is[] của jQuery để kiểm tra phần tử đã chọn với phần tử khác, bộ chọn hoặc bất kỳ đối tượng jQuery nào. Phương thức này duyệt dọc theo các phần tử DOM để tìm kết quả khớp, thỏa mãn tham số đã truyền. Nó sẽ trả về true nếu khớp, ngược lại trả về false

Tải lại vị trí [] trong JavaScript là gì?

Vị trí. phương thức reload[] tải lại URL hiện tại, giống như nút Làm mới .

JavaScript có thể sửa đổi Dom không?

DOM HTML cho phép JavaScript thay đổi nội dung của các phần tử HTML .

Vị trí cửa sổ có href tải lại trang không?

vị trí. reload[] tải lại trang hiện tại với dữ liệu POST , trong khi cửa sổ. địa điểm. href='url của bạn' không bao gồm dữ liệu POST.

Chủ Đề