search
HomeBackend DevelopmentPython TutorialJavaScript 如何获取闭包变量?

<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>
我也来抖个机灵:IE9 下 __proto__ 是无效的呢。所以在 Object.prototype 上添加 getter 的方法在 IE9 下依然有效。 如果你引擎实现得好的话,那么——是做不到的。你可以试着用霍尔逻辑证明一下……
不过如果引擎不支持 __proto__(似乎 IE 很长时间都没支持),可以通过注入 Object.prototype 一个 getter 实现。 原来是面试题,js都被这么玩坏了。
明明是通过闭包实现的私有,那就让他私有好了,为毛要访问他呢,看着不爽就重构一下。 我们可以通过return一个函数来获取,具体看JS的闭包机制。 你是想问js是否有类似反射这种反设计的东西吗?
意图限制而又开后门。。。 return 的对象中把person也返回不就行了 谢邀
闭包的核心点是作用链的引用,
所以return person就可以了。
不知道为什么会有这种需求 javascript里的闭包是设计成对外隐藏内部变量,这才是闭包安全性的意义所在,你现在要获取它,,,,,什么鬼
Statement
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
Python vs. C  : Applications and Use Cases ComparedPython vs. C : Applications and Use Cases ComparedApr 12, 2025 am 12:01 AM

Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

The 2-Hour Python Plan: A Realistic ApproachThe 2-Hour Python Plan: A Realistic ApproachApr 11, 2025 am 12:04 AM

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python: Exploring Its Primary ApplicationsPython: Exploring Its Primary ApplicationsApr 10, 2025 am 09:41 AM

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

How Much Python Can You Learn in 2 Hours?How Much Python Can You Learn in 2 Hours?Apr 09, 2025 pm 04:33 PM

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

How to teach computer novice programming basics in project and problem-driven methods within 10 hours?How to teach computer novice programming basics in project and problem-driven methods within 10 hours?Apr 02, 2025 am 07:18 AM

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading?How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading?Apr 02, 2025 am 07:15 AM

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

What should I do if the '__builtin__' module is not found when loading the Pickle file in Python 3.6?What should I do if the '__builtin__' module is not found when loading the Pickle file in Python 3.6?Apr 02, 2025 am 07:12 AM

Error loading Pickle file in Python 3.6 environment: ModuleNotFoundError:Nomodulenamed...

How to improve the accuracy of jieba word segmentation in scenic spot comment analysis?How to improve the accuracy of jieba word segmentation in scenic spot comment analysis?Apr 02, 2025 am 07:09 AM

How to solve the problem of Jieba word segmentation in scenic spot comment analysis? When we are conducting scenic spot comments and analysis, we often use the jieba word segmentation tool to process the text...

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.