`

Function Expressions scope

阅读更多
/*
官网原文:
         In JScript the identifier in a function expression is visible in the enclosing scope because such expressions are treated as function declarations. Example: 
*/
<script> 
       var foo = function bar(b) 
       { 
           if (b == true) 
           {
                bar(); // works because bar is visible inside itself 

           }   
           else 
           { 
               document.write("hello"); 
           } 
      } 
      foo(); 
      // works because foo is a ref to Function object bar(false); 
      // should fail because bar is not visible here 
</script>
/* Output: 
            IE: prints "hellohello" 
            FF: prints “hello” followed by syntax error (bar is not defined) 
            Opera: prints “hello” followed by ReferenceError (Reference to undefined variable: bar) 
            Safari: prints “hello”
*/

/*
Note: IE treats nested function expressions containing a function name as a function declaration that is scoped to the enclosing function (applying the second bullet rule from §10.1.3 instead of case 3 of the semantics in §13. In the following example the forward reference to x gets resolved in IE, whereas we get a syntax error in FF and TypeError in Opera. Example: 
*/
<script> 
function f(x) 
{ 
    x(); 
    y = function x() 
    {
         document.write("inner called ")
     }; 
     document.write(x);
     document.write(arguments[0]); 
} 
document.write("test 4 ");
 f("param"); 
</script>
/*
       Output: 
          IE: test 4 inner called function x() {document.write("inner called ")}function x() {document.write("inner called ")}
       FF: test 4 Opera: test 4 
       Safari: test 4 
Here is another example of the same issue: 
*/
<script> 
function foo() 
{ 
        function bar() {}
        var x = function baz(z) 
       { 
           document.write("baz" + z); 
           if (z) baz(false); 
       };
        x(true); // legal;
        bar();   // legal 
        baz(true); // should be illegal, is legal 
} 
foo(); 
</script>
/* The function statement bar should add “bar” to foo‟s variable object. The function expression baz should NOT add “baz” to foo‟s variable object, but it should be in scope when baz is called, so that it can do the recursive call. 
         Output: IE: baztruebazfalsebaztruebazfalse 
                 FF: baztruebazfalse (followed by an error – baz not defined) 
                 Opera: same as FF
*/
分享到:
评论

相关推荐

    Programming JavaScript Applications

    Callbacks, Promises and Deferreds, Code Quality, Function Polymorphism, Function Scope, Hoisting and Closures, Functional Programming and Stateless Functions, Immediately Invoked Function Expressions,...

    C程序设计语言第2版

    Pointers and Function Arguments Pointers and Arrays Address Arithmetic Character Pointers and Functions Pointer Arrays; Pointers to Pointers Multi-dimensional Arrays Initialization of Pointer ...

    plsqldev13.0.5.1908x32主程序+ v12中文包+keygen.rar

    32位版本的 PLSQL 正式版。...Test Window bind variable scan function could fail when using q'&lt;string&gt;' expressions Table.column highlighting did not always work within if-then-else statements

    The C programming Language(chm格式完整版)

    Pointers and Function Arguments Pointers and Arrays Address Arithmetic Character Pointers and Functions Pointer Arrays; Pointers to Pointers Multi-dimensional Arrays Initialization of Pointer ...

    plsqldev13.0.5.1908x64主程序+ v12中文包+keygen.rar

    64位版本的 PLSQL 正式版,只能运行在64...Test Window bind variable scan function could fail when using q'&lt;string&gt;' expressions Table.column highlighting did not always work within if-then-else statements

    Responsive Web Design with AngularJS

    link: function(scope, element, attrs) { // 动态调整导航栏样式 } }; }); ``` 3. **状态管理和路由**:AngularJS的路由机制支持单页面应用中不同视图之间的切换,通过结合媒体查询等技术,可以在不同设备上...

    Secrets of the JavaScript Ninja, 2nd Edition

    They inherit the `this` value from the enclosing context, eliminating common issues related to the `this` keyword in traditional function expressions. For example: ```javascript const values = [0, 3...

    javascript语言精粹(中英文版)

    The function Statement Versus the function Expression Section B.10. Typed Wrappers Section B.11. new Section B.12. void Appendix C. JSLint Section C.1. Undefined Variables and Functions Section...

    Invent Your Own Computer Games with Python (2008).pdf

    Variable Scope x Parameters x Local Variables and Global Variables with the Same Name x Where to Put Function Defintions x The Colon : x Step by Step, One More Time x Designing the Program x A Web ...

    AngularJS一些实例代码

    app.controller('myCtrl', function($scope) { $scope.message = "Hello, AngularJS!"; }); ``` 这段代码展示了如何创建一个名为`myApp`的应用,并定义了一个`myCtrl`控制器,将`message`变量绑定到视图上。...

    11g_plsql_user_guide_and_reference.pdf

    The PL/SQL function result cache is a new feature that caches the results of a function call based on its input parameters. This can significantly improve the performance of applications by avoiding ...

    angularjs-code.zip

    app.controller('MyCtrl', function($scope) { $scope.name = 'John'; }); ``` ### 4. $scope $scope是连接控制器和视图的桥梁,它是应用程序中数据的容器。它提供了事件广播、watch等功能,帮助我们管理数据和...

    对angular 实时更新模板视图的方法$apply详解

    2. **Scope作为Expressions的上下文**:在模板中,我们可以使用表达式(Expressions)来访问Scope中的数据。例如,`{{ myVar }}`会显示`$scope.myVar`的值。表达式的计算依赖于当前作用域的环境。 3. **Scope的事件...

    C Programming

    - **Function Heading**: Explanation of the syntax for defining a function, including return type, name, and parameters. - **Function Body**: Description of the block of code that performs the actual ...

    The C programming Language

    Call by Value Character Arrays External Variables and Scope &lt;br&gt;Chapter 2: Types, Operators and Expressions Variable Names Data Types and Sizes Constants Declarations ...

    AngularJS 中文API参考手册.zip_API_angularjs_angularjs api

    myApp.controller('MyController', function($scope) { $scope.message = 'Hello, AngularJS!'; }); ``` **三、服务(Service)** 服务是可复用的、单例的对象,它们提供跨控制器的数据共享和功能封装。AngularJS...

    oracle-pl-sql-programming-5th-edition

    including the edition-based redefinition capability, the function result cache, the new CONTINUE statement, fine-grained dependency tracking, sequences in PL/SQL expressions, supertype invocation ...

Global site tag (gtag.js) - Google Analytics