How does JavaScript obtain closure variables?

Source: Internet
Author: User
Varo (function () {varperson {letperson will also be used to get the external let face. name: Vincent, age: 24 ,__ proto __: null yes, you are not mistaken. It really points to null}; return {run: function (k) {returnperso
Var o = (function () {var person = {// let person will be used externally to get the let face. name: 'Vincent ', age: 24, __proto _: null // yes. You are not mistaken. It really points to null}; return {run: function (k) {return person [k] ;}} (); // then the problem arises. The excavator technology is incorrect!
Reply content: Shake your mind. Let's talk about it seriously before shaking:

The closure of JavaScript is a very tight encapsulation. It can be said that closures are the only reliable implementation method of "private" Access Control before ES6's private Symbol.

At the JavaScript layer, there is no way to retrieve the variables captured by the closure through the closure function object-unless the function itself returns it (or passes it out in other ways ). In this way, closures can precisely control the amount of information they want to expose.

For example:

var o = (function () {  var a = 1  var b = 42  return {    foo: function () {      console.log(a + b)      return b    }  }})()
I also want to shake my mind: _ proto _ in IE9 is invalid. Therefore, the getter method added to Object. prototype is still valid in IE9. If your engine is well implemented, you cannot. You can try to prove it with Hall logic ......
However, if the engine does not support _ proto _ (it seems that IE has not supported it for a long time), you can inject Object. prototype into a getter. It turned out to be a face-to-face question, and JavaScript was so bad.
Obviously, it is private implemented through the closure, so let him be private. If Mao wants to access him, restructures it if he looks unhappy. We can get it by returning a function. For details, refer to the closure mechanism of JS. . Do you want to ask if js has anti-design such as reflection?
Attempt to restrict and open a backdoor... If the person is returned in the return object, thank you for your invitation.
The core point of the closure is the reference of the function chain,
So return person is fine.
I don't know why there is such a requirement. The closure in javascript is designed to hide internal variables externally. This is the meaning of closure security. You need to get it now

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.