Table of Contents
回复内容:
Home Backend Development Python Tutorial JavaScript 如何获取闭包变量?

JavaScript 如何获取闭包变量?

Jun 06, 2016 pm 04:22 PM
function null return var

<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>
Copy after login

回复内容:

来抖个机灵。抖前先把正经的说了:

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>
Copy after login
我也来抖个机灵:IE9 下 __proto__ 是无效的呢。所以在 Object.prototype 上添加 getter 的方法在 IE9 下依然有效。 如果你引擎实现得好的话,那么——是做不到的。你可以试着用霍尔逻辑证明一下……
不过如果引擎不支持 __proto__(似乎 IE 很长时间都没支持),可以通过注入 Object.prototype 一个 getter 实现。 原来是面试题,js都被这么玩坏了。
明明是通过闭包实现的私有,那就让他私有好了,为毛要访问他呢,看着不爽就重构一下。 我们可以通过return一个函数来获取,具体看JS的闭包机制。 你是想问js是否有类似反射这种反设计的东西吗?
意图限制而又开后门。。。 return 的对象中把person也返回不就行了 谢邀
闭包的核心点是作用链的引用,
所以return person就可以了。
不知道为什么会有这种需求 javascript里的闭包是设计成对外隐藏内部变量,这才是闭包安全性的意义所在,你现在要获取它,,,,,什么鬼
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Detailed explanation of the usage of return in C language Detailed explanation of the usage of return in C language Oct 07, 2023 am 10:58 AM

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.

What does function mean? What does function mean? Aug 04, 2023 am 10:33 AM

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.

What is the execution order of return and finally statements in Java? What is the execution order of return and finally statements in Java? Apr 25, 2023 pm 07:55 PM

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

What is the difference between null and NULL in c language What is the difference between null and NULL in c language Sep 22, 2023 am 11:48 AM

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.

What do undefined and null mean? What do undefined and null mean? Nov 20, 2023 pm 02:39 PM

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.

What is the purpose of the 'enumerate()' function in Python? What is the purpose of the 'enumerate()' function in Python? Sep 01, 2023 am 11:29 AM

In this article, we will learn about enumerate() function and the purpose of “enumerate()” function in Python. What is the enumerate() function? Python's enumerate() function accepts a data collection as a parameter and returns an enumeration object. Enumeration objects are returned as key-value pairs. The key is the index corresponding to each item, and the value is the items. Syntax enumerate(iterable,start) Parameters iterable - The passed in data collection can be returned as an enumeration object, called iterablestart - As the name suggests, the starting index of the enumeration object is defined by start. if we ignore

What is the difference between null and undefined What is the difference between null and undefined Nov 08, 2023 pm 04:43 PM

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.

Let's talk about the differences between var, let and const (code example) Let's talk about the differences between var, let and const (code example) Jan 06, 2023 pm 04:25 PM

This article brings you relevant knowledge about JavaScript. It mainly introduces the differences between var, let and const, as well as the relationship between ECMAScript and JavaScript. Interested friends can take a look at it. I hope Helpful to everyone.

See all articles