`
tapestry
  • 浏览: 187041 次
社区版块
存档分类
最新评论

javascipt scope

阅读更多

From Apress ProJavaScriptTechniques

In JavaScript, scope is kept within functions, but not within blocks (such as while, if, and for statements).

js 代码
 
  1. // Set a global variable, foo, equal to test  
  2. var foo = "test";  
  3. // Within an if block  
  4. if ( true ) {  
  5. // Set foo equal to 'new test'  
  6. // NOTE: This is still within the global scope!  
  7. var foo = "new test";  
  8. }  
  9. // As we can see here, as foo is now equal to 'new test'  
  10. alert( foo == "new test" );  
  11. // Create a function that will modify the variable foo  
  12. function test() {  
  13. var foo = "old test";  
  14. }  
  15. // However, when called, 'foo' remains within the scope  
  16. // of the function  
  17. test();  
  18. // Which is confirmed, as foo is still equal to 'new test'  
  19. alert( foo == "new test" );  

An interesting aspect of browser-based JavaScript is that all globally scoped variables are actually just properties
of the window object.

js 代码
  1. // A globally-scoped variable, containing the string 'test'   
  2. var test = "test";   
  3. // You'll notice that our 'global' variable and the test   
  4. // property of the the window object are identical   
  5. alert( window.test == test );  

In Listing 2-12 a value is assigned to a variable (foo) within the scope of the test() function. However, nowhere in Listing 2-12 is the scope of the variable actually declared (using var foo). When the foo variable isn’t explicitly defined, it will become defined globally, even though it is only used within the context of the function scope.

Listing 2-12

js 代码
  1. // A function in which the value of foo is set   
  2. function test() {   
  3. foo = "test";   
  4. }   
  5. // Call the function to set the value of foo   
  6. test();   
  7. // We see that foo is now globally scoped   
  8. alert( window.foo == "test" );  
分享到:
评论

相关推荐

    JavaScript scope chain

    Study note on scope chain in JavaScript

    面向对象的 JavaScript 编程及其 Scope 处理

    面向对象的 JavaScript 编程及其 Scope 处理,论述了Javascript面向对象的实现原理和技术细节,详细描述了JavaScript的对象模型。

    What-is-Scope.pdf.zip_javascript

    You Don t Know JavaScript: Scope and Closures - What is Scope

    深入理解JavaScript系列

    深入理解JavaScript系列(14):作用域链(Scope Chain) 深入理解JavaScript系列(15):函数(Functions) 深入理解JavaScript系列(16):闭包(Closures) 深入理解JavaScript系列(17):面向对象编程之一般...

    深入理解JavaScript系列(.chm)

    深入理解JavaScript系列(14):作用域链 Scope Chain 深入理解JavaScript系列(15):函数(Functions) 深入理解JavaScript系列(16):闭包(Closures) 深入理解JavaScript系列(17):面向对象编程之一般...

    浅谈JavaScript的全局变量与局部变量

    一、JavaScript scope 的划分标准是function函数块,不是以 if、while、for来划分的 [removed] function f1(){ alert("before for scope:"+i); //i未赋值(并不是没有声明!使用未声明变量或函数会导致...

    javascript作用域链(Scope Chain)初探.docx

    javascript作用域链(Scope Chain)初探.docx

    面向对象的_JavaScript_编程及其_Scope_处理

    面向对象的_JavaScript_编程及其_Scope_处理

    javascript面向对象编程指南 2nd

    In depth discussion of functions, function usage patterns, and variable scope Understand how prototypes work Reuse code with common patterns for inheritance Make your programs cleaner, faster and ...

    Programming JavaScript Applications

    , AMD, Asynchronous Operations, Callbacks, Promises and Deferreds, Code Quality, Function Polymorphism, Function Scope, Hoisting and Closures, Functional Programming and Stateless Functions, ...

    Beginning.JavaScript.5th.Edition

    Chapter 4: Functions And Scope Chapter 5: Javascript-An Object-Based Language Chapter 6: String Manipulation Chapter 7: Date, Time, And Timers Chapter 8: Programming The Browser Chapter 9: Dom ...

    Eloquent JavaScript 3rd 第三版高清文字版

    This third edition introduces new features covering the 2017 version of JavaScript, such as class notation, arrow functions, iterators, async functions, template strings, and black scope. Author ...

    scope-chains-closures, Javascript作用域链和闭包 workshop.zip

    scope-chains-closures, Javascript作用域链和闭包 workshop 范围链和闭包 workshop正在启动$ npm install -g scope-chains-closures$ scope-chains-closures # or, shorter: sccjs使用箭头

    javascript作用域链(Scope Chain)用法实例解析

    主要介绍了javascript作用域链(Scope Chain)用法,结合实例形式较为详细的分析了javascript作用域链(Scope Chain)的概念、功能与相关使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下

    scope-analyzer:javascript AST的简单范围分析

    javascript AST的简单范围分析。 跟踪范围并收集对变量的引用。 注意事项和/或待办事项: 可能缺少边缘情况。 根本不考虑label: s之类的东西,但是理想情况下将来会考虑! 安装 npm install scope-analyzer 用法 ...

    Scope(作用域).md

    Scope(作用域)

    Eloquent Javascript, 3rd Edition

    This third edition introduces new features covering the 2017 version of JavaScript, such as class notation, arrow functions, iterators, async functions, template strings, and black scope. Author ...

    JavaScript 作用域scope简单汇总

    主要介绍了JavaScript 作用域scope简单汇总,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    javascript中的作用域scope介绍

    而在javascript中,变量的作用域是按函数来划分的——变量在某个函数范围内有效。比如: 代码如下: var f = false; if(true) { var f = true; } //此时f位于if内,也就是块内,等价于还是全局范围内 alert(f&#41...

Global site tag (gtag.js) - Google Analytics