JavaScript 如何获取闭包变量?
<span class="kd">var</span> <span class="nx">o</span> <span class="o">=</span> <span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span> <span class="kd">var</span> <span class="nx">person</span> <span class="o">=</span> <span class="p">{</span> <span class="c1">// let person 竟然也会被外部拿到 let 被打脸了啊</span> <span class="nx">name</span><span class="o">:</span> <span class="s1">'Vincent'</span><span class="p">,</span> <span class="nx">age</span><span class="o">:</span> <span class="mi">24</span><span class="p">,</span> <span class="nx">__proto__</span> <span class="o">:</span> <span class="kc">null</span> <span class="c1">// 是的你没有 看错 真的是 指向 null</span> <span class="p">};</span> <span class="k">return</span> <span class="p">{</span> <span class="nx">run</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">k</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="nx">person</span><span class="p">[</span><span class="nx">k</span><span class="p">];</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}());</span> <span class="c1">// 那么问题来了, 挖掘机技术 呸呸呸 说错了 口误! </span>
回复内容:
来抖个机灵。抖前先把正经的说了:JavaScript的闭包是一种颇为紧密的封装。可以说,闭包是JavaScript在ES6的private Symbol之前唯一靠谱的“private”访问控制的实现方式。
在JavaScript层面没有任何办法可以通过闭包的函数对象取出闭包捕获的变量——除非该函数自己把它返回出来(或者用其它方式传递出来)。这样闭包就可以精确的控制自己想要暴露出来的信息量。
所以例如说:
<span class="kd">var</span> <span class="nx">o</span> <span class="o">=</span> <span class="p">(</span><span class="kd">function</span> <span class="p">()</span> <span class="p">{</span> <span class="kd">var</span> <span class="nx">a</span> <span class="o">=</span> <span class="mi">1</span> <span class="kd">var</span> <span class="nx">b</span> <span class="o">=</span> <span class="mi">42</span> <span class="k">return</span> <span class="p">{</span> <span class="nx">foo</span><span class="o">:</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">a</span> <span class="o">+</span> <span class="nx">b</span><span class="p">)</span> <span class="k">return</span> <span class="nx">b</span> <span class="p">}</span> <span class="p">}</span> <span class="p">})()</span>
不过如果引擎不支持 __proto__(似乎 IE 很长时间都没支持),可以通过注入 Object.prototype 一个 getter 实现。 原来是面试题,js都被这么玩坏了。
明明是通过闭包实现的私有,那就让他私有好了,为毛要访问他呢,看着不爽就重构一下。 我们可以通过return一个函数来获取,具体看JS的闭包机制。 你是想问js是否有类似反射这种反设计的东西吗?
意图限制而又开后门。。。 return 的对象中把person也返回不就行了 谢邀
闭包的核心点是作用链的引用,
所以return person就可以了。
不知道为什么会有这种需求 javascript里的闭包是设计成对外隐藏内部变量,这才是闭包安全性的意义所在,你现在要获取它,,,,,什么鬼

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The usage of return in C language is: 1. For functions whose return value type is void, you can use the return statement to end the execution of the function early; 2. For functions whose return value type is not void, the function of the return statement is to end the execution of the function. The result is returned to the caller; 3. End the execution of the function early. Inside the function, we can use the return statement to end the execution of the function early, even if the function does not return a value.

Function means function. It is a reusable code block with specific functions. It is one of the basic components of a program. It can accept input parameters, perform specific operations, and return results. Its purpose is to encapsulate a reusable block of code. code to improve code reusability and maintainability.

Source code: publicclassReturnFinallyDemo{publicstaticvoidmain(String[]args){System.out.println(case1());}publicstaticintcase1(){intx;try{x=1;returnx;}finally{x=3;}}}#Output The output of the above code can simply conclude: return is executed before finally. Let's take a look at what happens at the bytecode level. The following intercepts part of the bytecode of the case1 method, and compares the source code to annotate the meaning of each instruction in

The difference between null and NULL in C language is: null is a macro definition in C language, usually used to represent a null pointer, which can be used to initialize pointer variables, or to determine whether the pointer is null in a conditional statement; NULL is a macro definition in C language A predefined constant in , usually used to represent a null value, used to represent a null pointer, null pointer array or null structure pointer.

In JavaScript, both undefined and null represent the concept of "nothing": 1. undefined represents an uninitialized variable or a non-existent property. When a variable is declared but no value is assigned to it, the value of the variable is undefined , when accessing properties that do not exist in the object, the returned value is also undefined; 2. null represents an empty object reference. In some cases, the object reference can be set to null to release the memory it occupies.

Both null and undefined indicate a lack of value or an undefined state. Depending on the usage scenario, there are some guiding principles for choosing to use null or undefined: 1. When you need to clearly indicate that a variable is empty or invalid, you can use null; 2. When a variable has been declared but not yet assigned a value, it will be set to undefined by default; 3. When you need to check whether a variable is empty or undefined, use the strict equality operator "===" to determine whether the variable is null or undefined. .

The difference between null and undefined is: 1. Semantic meaning; 2. Usage scenarios; 3. Comparison with other values; 4. Relationship with global variables; 5. Relationship with function parameters; 6. Nullability check; 7. Performance considerations; 8. Performance in JSON serialization; 9. Relationship with types. Detailed introduction: 1. Semantic meaning, null usually means knowing that this variable will not have any valid object value, while undefined usually means that the variable has not been assigned a value, or the object does not have this attribute; 2. Usage scenarios, etc.

Detailed explanation of the role and function of the MySQL.proc table. MySQL is a popular relational database management system. When developers use MySQL, they often involve the creation and management of stored procedures (StoredProcedure). The MySQL.proc table is a very important system table. It stores information related to all stored procedures in the database, including the name, definition, parameters, etc. of the stored procedures. In this article, we will explain in detail the role and functionality of the MySQL.proc table
