`
指甲刀X
  • 浏览: 33916 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

对javascript中的Variable Object的理解

阅读更多

      在ECMAScipt中,通过变量对象Variable Object (VO)机制来存贮、访问变量variables。VO中包含了:

                  1. variables (通过var 声明的变量),

                  2. function declaration (函数声明),

                  3. function formal parameters (函数行参)。

                  注: 不包括function expression (函数表达式)。


      Variable Object在Global Context中等同于Global Object,在Function Context中称为Activation Object (AO),一般来讲,Variable Object / Activation Object 我们不能直接在外部访问到。


        要访问某个变量x,首先在x所在Function Context中的Variable Object中查找,若找不到,则在x所属function的[[scope]]属性中保存的scope chain中,逐级向上查找,直至查找到Global Object,如果仍未找到则返回 x is not defined


       我们所常见的window对象实际上是Global Object的引用,所以通过var在Global Context中声明的变量x,可以通过x直接访问,也可通过window.x来间接访问,并且通过window.x访问比直接访问要慢
      

var a = "variable";
b = "property";

console.log(window.a);  // "variable"
console.log(window.b); // "property"

delete window.a;  // false
console.log(window.a); // "variable"

delete window.b; //true
console.log(window.b);  //undefined;
 

        在上面的例子中, 虽然都能通过window.a, window.b的形式来访问,看似都是window的属性,但是实质是不同的。 不使用var声明的”变量“,实际不是真正的变量,而是Global Object的属性,可以通过delete 关键字删除,而真正的变量拥有”DontDelete“属性,不能通过delete删除。
        但值得注意的是,在firebug中,执行delete window.a; delete window.b;的返回值都是true,这是由于在eval context中,变量不会被添加”DontDelete”属性,firebug正是利用eval来执行我们在console中的代码,而在chrome,opera的console中则返回正确的false,true
参考:http://dmitrysoshnikov.com/ecmascript/chapter-2-variable-object/

分享到:
评论

相关推荐

    深入理解JavaScript系列

    深入理解JavaScript系列(12):变量对象(Variable Object) 深入理解JavaScript系列(13):This? Yes, this! 深入理解JavaScript系列(14):作用域链(Scope Chain) 深入理解JavaScript系列(15):函数...

    Javascript.Object.Oriented.Programming.pdf

    Build sophisticated web applications by mastering the art of Object-Oriented Javascript About This Book Learn popular Object-Oriented programming (OOP) principles and design patterns to build robust ...

    深入理解JavaScript系列(.chm)

    深入理解JavaScript系列(12):变量对象(Variable Object) 深入理解JavaScript系列(13):This Yes this 深入理解JavaScript系列(14):作用域链 Scope Chain 深入理解JavaScript系列(15):函数...

    深入理解JavaScript系列(12) 变量对象(Variable Object)

    Soshnikov 发布时间:2009-06-27 俄文地址:http://dmitrysoshnikov.com/ecmascript/ru-chapter-2-variable-object/ 英文翻译:Dmitry A. Soshnikov 发布时间:2010-03-15 英文地址:...

    javascript面向对象编程指南 2nd

    javascript面向对象编程指南 2nd英文版,英文名:Object-Oriented JavaScript。 What you will learn from this book The basics of object-oriented programming, and how to apply it in the JavaScript ...

    深入理解JavaScript系列.chm

    12.变量对象(Variable Object) 13.This? Yes,this! 14.作用域链(Scope Chain) 15.函数(Functions) 16.闭包(Closures) 17.面向对象编程之一般理论 18.面向对象编程之ECMAScript实现 19.求值策略 20.《你真懂...

    Effective JavaScript: 68 Specific Ways to Harness the Power of JavaScript[EPUB版]

    Precise and practical explanations of JavaScript’s functions and variable scoping semantics Useful JavaScript programming patterns and idioms, such as options objects and method chaining In-depth ...

    javascript 基础 GIF套图

    JavaScript-array数组.gif JavaScript-function-base函数基础.gif Javascript-operational-character运算符.gif ...The-JavaScript-variable变量.gif Window-object对象.gif DOM-operation基本操作.gif

    Mastering.JavaScript.1785281348

    - Code using the powerful object-oriented feature in JavaScript - Test and debug your code using JavaScript strategies - Master DOM manipulation, cross-browser strategies, and ES6 - Understand the ...

    Professional JavaScript for Web Developers, 3rd Edition

    This book is aimed at three groups of readers: Experienced object-oriented programming developers looking to learn JavaScript as it relates to traditional OO languages such as Java and C++; Web ...

    JavaScript: Moving to ES2015

    You will gain a concrete understanding of variable scoping, loops, and best practices on using types and data structures, as well as the coding style and recommended code organization patterns in ...

    JavaScript权威指南

    Object-Oriented JavaScript Section 8.6. Objects as Associative Arrays Section 8.7. Object Properties and Methods Chapter 9. Arrays Section 9.1. Arrays and Array Elements Section 9.2. ...

    Mastering JavaScript(PACKT,2016)

    Code using the powerful object-oriented feature in JavaScript Test and debug your code using JavaScript strategies Master DOM manipulation, cross-browser strategies, and ES6 Understand the basic ...

    javascript权威指南(第六版)

    9.6 Object-Oriented Techniques in JavaScript 215 9.7 Subclasses 228 9.8 Classes in ECMAScript 5 238 9.9 Modules 246 10. Pattern Matching with Regular Expressions . . . . . . . . . . . . . . . . . . . ...

    JavaScript权威指南(第6版,英文

    JavaScript权威指南(第6 Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii 1. Introduction to ...

    讲解JavaScript中for…in语句的使用方法

    但是,一旦你会对JavaScript对象了解后,那么会发现这个循环非常有用。 语法 for (variablename in object){ statement or block to execute } 从对象每次迭代一个属性分配给变量名(variablename),这个循环持续...

    Javascript核心读书有感之类型、值和变量

    计算机程序的运行需要对值(value)比如数字3.14或者文本”hello world”进行操作,在编程语言中,能够表示并...javascript的数据分为两类:原始类(primitive type)和对象类型(object type) javascript中的原始类包括数字

    图文详解Javascript中的上下文和作用域

    一段程序可能被分割成许多不同的上下文,每一个上下文都会绑定一个变量对象(variable object),它就像一个容器,用来存储当前上下文中所有已定义或可获取的变量、函数等。位于最顶端或最外层的上下文称为全局上...

    JavaScript高级程序设计(第3版)学习笔记8 js函数(中)

    6、执行环境和作用域 (1)执行环境(execution context):所有的JavaScript代码... (2)变量对象(variable object):每一个执行环境都有一个与之对应的变量对象,执行环境中定义的所有变量和函数就是保存在这个变

Global site tag (gtag.js) - Google Analytics