简体   繁体   English

标识符解析如何在Javascript中工作?

[英]How does the identifier resolution work in Javascript?

Can someone please explain how identifier resolution works for Javascript taking the following scenarios : 有人可以在以下情况下解释标识符解析对于Javascript的工作方式:

  1. A global function is called . 全局函数称为。
  2. A function inside an user defined javascript object is called . 用户定义的javascript对象中的函数称为。

You have to learn the context and scoping in ECMA Script to understand this. 您必须学习ECMA脚本中的上下文和作用域才能理解这一点。 And in between is it identifier or variable? 介于两者之间的是标识符还是变量?

Identifiers are normally tagged to window object. 标识符通常标记为窗口对象。 Say String() it's actually window.String(). 说String()实际上是window.String()。

Normally when a page loads the all the variables and functions are added as properties to the window object. 通常,当页面加载时,所有变量和函数都作为属性添加到窗口对象。 The window object becomes the global context(GC) in the case of browsers. 对于浏览器,窗口对象成为全局上下文(GC)。 So when a global function(this is just a method nothing is there like a global function) is called the variable is just looked in the global context. 因此,当全局函数(这只是一个方法,没有什么像全局函数一样)被称为变量时,它只是在全局上下文中查找。 And thus it'll identify the variable. 因此,它将识别变量。

But if there is a function inside which an inner function is there then identifying a global variable will be two folds first it'll check in function scope and then in global scope. 但是,如果内部有一个内部函数,则标识一个全局变量将是双重的,它将首先检查函数范围,然后检查全局范围。 If the variable is a function variable. 如果变量是函数变量。 Then the variable will always be there in the functions execution context(EC) 然后变量将始终存在于函数执行上下文(EC)中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM