


Thoughts caused by a piece of code
var laterDeclaredVar = 'I am a global variable ...'; (function(){ console.log(laterDeclaredVar); //会输出什么? laterDeclaredFunction(); //能否被正常执行? var laterDeclaredVar = 'I am a variable which is defined later...'; function laterDeclaredFunction () { console.log('I can be called any where though I am declared later...'); // 上面打印的话翻译一下是:虽然我被晚一些定义了,但是我可以在函数的任何地方被访问 // I can be called any where even though I am declared later // 后者则被翻译为:即使我被晚一些定义了,我可以在函数的任何地方被访问 // 这里需要补充一个知识点是though和even though的区别: // though和even though都引导让步状语从句,但是though和even意为“虽然” // though引导的句子所说的是事实 // 而even though(=even if),意为“即使” // 有退一步设想的意味,所说的不一定是事实。 // 为了写这个打印专门复习了一下英文,查了一下这个句式该怎么写,都是知识点,快快拿小本记好! } })()
What this code requires us to think about is what the third line will output on the console ? Can the call to laterDeclaredFunction() in the fourth line succeed?
The answer is Jiang Zi’s:
We all have a consensus that the execution order of js language is automatic From top to bottom, what if the variable laterDeclaredVar has been declared and defined on the first line, but undefined is printed on the third line? Why is laterDeclaredFunction declared on the seventh line, but the call on the fourth line succeeds? Why is this so?
It’s all about variable objects! !
First go back to the execution context life cycle part to review.
.... Review completed!
What is a variable object?
When we write a program, we will define many variables and functions, and the essence of the above problem is actually how and where for the interpreter. Find these variables and functions?
The variable object is a concept corresponding to the execution context, which defines all variables, functions under the execution context, and the parameter list of the current execution context function. That is to say, the variable object defines the parameter list, internal variables and internal functions defined within a function.
In terms of data structure, it is like this:
#The creation process of variable objects?
Take the following code as an example to see the creation process of outerFun() variable object.
function outerFun (arg1, arg2) { var outerV1 = 1 var outerV2 = 2 function innerFun1 () { var innerV1 = 3; var innerV2 = 4; console.log('i am innerFun1...') } function innerFun2 () { console.log('i am innerFun2...') } function outerV2 () { return 'i am outerV2' } } outerFun()
Its variable object creation process is like this:
The variable object is created when the function is called but before the function is executed. The process of creating the variable object is actually the data within the function. (Function parameters, internal variables, internal functions) initialization process.
What is an active object?
Before entering the execution phase, the properties in the variable object cannot be accessed! But after entering the execution phase, the variable object is transformed into an active object, and the properties inside can be accessed, and then the execution phase operations begin. So the active object is actually another form of the variable object when it is actually executed.
Global variable object
We are above We are talking about variable objects in the function context, and their contents are determined based on the data (parameters, variables, functions) in the execution context. The variable objects in the global context are different. Taking the browser as an example, the global variable object is the window object. During the initialization phase of the global context before execution, global variables and functions are mounted on the window.
#At this point, using the knowledge of variable objects to explain the execution results of the code at the beginning of the article, I have already solved the problem of Ren Duer pulse. No detailed analysis will be done here.
At this point, the variable objects and active objects in the execution context life cycle have been sorted out. Let’s talk about the next thing!
The above is the detailed content of Detailed graphic explanation of Javascript variable objects and active objects. For more information, please follow other related articles on the PHP Chinese website!

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、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

Dreamweaver Mac version
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
