[Javascript] Private Variables with IIFEs

An IIFE (immediately invoked function expression) is when a function is called immediately after it is defined. These functions are defined and called once and are not accessed by other functions, variables, or from the global namespace. This is because it uses the grouping operator to make these function declarations an expression. Because of this one time invocation and lack of accessibility, we can use their function scope to hide variables from being access from anywhere else.

(function findName() {
  return 'hello'
})()

findName() // error

猜你喜欢

转载自www.cnblogs.com/Answer1215/p/12003833.html