`

Function Declarations within bodies

阅读更多
/*
官网原文:
When a function is defined using a function declaration, JScript binds the function to the global scope regardless of any scope changes introduced by with clauses.
*/
        var v = 'value 1'; 
	var o = {v: 'value 2'};
	function f1() 
	{ 
		alert('v == ' + v); 
	};
	with (o) 
	{ 
		function f2() 
		{ 
			alert('v == ' + v); 
		};
	}
	 // call with the initial values 
	f1(); 
    f2(); 
    // now modify the values 
    v = 'modified value 1';
    o.v = 'modified value 2'; 
    f1(); 
    f2();
/*
Output: 
     IE: v == value 1
         v == value 1 
         v == modified value 1 
         v == modified value 1 
     FF: v == value 1 
         v == value 2 
         v == modified value 1 
         v == modified value 2 
     Opera: same as IE 
     Safari: same as FF
*/
/*
官网原文:
Contrast the above example with the case where the function is defined as a function expression:
*/
    var v = 'value 1'; 
    var o = {v: 'value 2'};
    var f1 = function () 
    { 
    	alert('v == ' + v); 
    }; 
    with (o) 
    { 
    	var f2 = function () 
    	{ 
    		alert('v == ' + v); 
    	}; 
    } // call with the initial values 
    f1(); 
    f2(); 
    // now modify the values
    v = 'modified value 1'; 
    o.v = 'modified value 2'; 
    f1(); 
    f2();
/*
Output: IE, FF, Opera, Safari:
        v == value 1 
        v == value 2 
        v == modified value 1 
        v == modified value 2
*/


// case 1
var o = false; 
	with ({o : true})
	{ 
		function f()
		{ 
			return o; 
		}
	} 
	document.write("FunDecl in with-smt is run time instantiated? " +f() +" (should be: true)");
	document.write("<br>");

// case 2
var usebeforedeclare = (typeof fn === 'function');
	if (true)
	{ 
			function fn() { return true; }
	} 
	else
	{ 
		function fn() { return false; }
	} 
	document.write("FunDecl in if-smt is parse time instantiated? " +usebeforedeclare +" (should be: false)\nThe correct path is taken: " +fn() +" (should be: true)");
	document.write("<br>");

// case 3
BRANCH : 
	{
		break BRANCH; 
		function fnc(){}
	} 
	document.write("FunDecl in labelled statement is parse time instantiated? " +(typeof fnc === 'function') +" (should be: false)"); 
	document.write("<br>");

// case 4
while (false) 
	function func(){} 
	document.write("FunDecl in while-smt is parse time instantiated? " +(typeof func === 'function')+ " (should be: false)"); 
	document.write("<br>");
    

// case 5
for ( ; false; )
	function funct(){} 
	document.write("FunDecl in for-smt is parse time instantiated? " +(typeof funct === 'function') +" (should be: false)"); 
	document.write("<br>");

// case 6
for(var t in {}) function functi(){}
	document.write("FunDecl in for..in-smt is parse time instantiated? " +(typeof functi === 'function') +" (should be: false)"); 
	document.write("<br>");

// case 7
try { } catch(e) { function functio(){} }
	document.write("FunDecl in try..catch-smt is parse time instantiated? " +(typeof functio === 'function') +" (should be: false)");
	document.write("<br>");

// case 8
if (true) 
	{
		function bar() { document.write("bar1"); }
	}
	else 
	{
	    function bar() { document.write("bar2"); }
	}
	bar();
	function foo() 
	{ 
	    if (true) 
		{
		      function baz() { document.write("baz1"); }
	    }
		else 
		{ 
		     function baz() { document.write("baz2"); }
		} 
		baz();
	}
	foo();

分享到:
评论

相关推荐

    Flex4 Declarations in ActionScript

    override protected function createChildren():void { super.createChildren(); var button:Button = new Button(); button.label = label; addChild(button); } } ``` 在这个例子中,我们创建了一个名为...

    screen_info.rar__GABIXX_NOEXCEPT

    Use _GABIXX_NOEXCEPT to use the equivalent of the C++11 noexcept qualifier at the end of function declarations.

    Source-Navigator

    The database represents internal program structures, locations of function declarations, contents of class declarations, and relationships between program components. Source-Navigator graphical ...

    string.h库文件

    /*** *string.h - declarations for string manipulation functions ...* This file contains the function declarations for the string * manipulation functions. * [ANSI/System V] * * [Public] * ****/

    Placing const in Declarations by Dan Saks

    "Placing const in Declarations by Dan Saks"这篇文章很可能是深入探讨了如何在声明中有效地使用`const`。 1. `const`的基本概念: - `const`关键字用来定义一个只读变量,一旦赋值后就不能再次更改。 - 它可以...

    Google C++ Style Guide_英文版.pdf

    - **Function Declarations and Definitions:** Separate function declarations and definitions clearly. - **Lambda Expressions:** Format lambda expressions consistently. - **Floating-point Literals:** ...

    issrc-5.3.3.zip_The Front_delphi inno setup

    编译通过的Inno setup Inno Setup consists of five projects: Compil32.dpr - This is the GUI front-end for the compiler....various structures and function declarations used to interface to the DLL.

    Oracle数据库开发实用教程第7章.ppt

    其中,`package_name` 是包名,`public_variable_declarations` 是公共变量声明,`public_type_declarations` 是公共类型声明,`public_exception_declarations` 是公共异常声明,`function_declarations` 是函数...

    IDC Scripts Pack v0.1_IDCScript_scripts.pack_

    This file contains IDA built-in function declarations and internal bit definitions. Each byte of the program has 32-bit flags (low 8 bits keep the byte value). These 32 bits are used in get_full_flags...

    css-declarations:解析和字符串化CSS声明

    : npm install css-declarations用import { parse , stringify } from 'css-declarations'var values = parse ( ` color:/*red*/purple; -webkit-border-radius: 3px !important;;` )// =&gt; {color: 'purple', ...

    C++课程设计之银行管理系统

    #include #include #include #include #include #include using namespace std; // Function declarations void write_account(); void display_account(in ........

    Oracle数据库开发实用教程第7章.pptx

    | function_declarations | procedure_specifications END [package_name] ``` 其中:package_name是包名。public_variable_declarations是公共变量声明。public_type_declarations是公共类型声明public_exception...

    2-variable-declarations(变量声明2).pdf

    function f() { var a = 10; return function g() { var b = a + 1; return b; } } var g = f(); g(); // returns 11; ``` 然而,`var`声明有一些特殊的规则,可能会导致混淆和错误。例如,`var`声明的提升...

    DBGrid3D (增加了鼠标滚轮的支持)

    { Private declarations } protected { Protected declarations } public { Public declarations } published function DoMouseWheel(Shift: TShiftState; WheelDelta: Integer; MousePos: TPoint): ...

    JS两种定义方式的区别、内部原理

    代码如下: // 方式1 function func1(x,y){ // your code } // 方式2 var func2 = function(x,y){ // your code } 方式1 是典型的函数声明(Function declarations)。 方式2 是函数表达式(Function expressions),将...

    RACLE_的过程,函数,包等创建

    - **`function_declarations`** 和 **`procedure_specifications`**:声明包中的函数和过程。 ##### 包主体 (Package Body) 包主体包含了包中所有函数和过程的具体实现细节。 ##### 创建包主体的语法: ```sql ...

    cucu-a compiler you can understand

    - **Program**: A program is defined as a repeating sequence of variable declarations, function declarations, and function definitions. - `&lt;program&gt;::={||&lt;func-def&gt;}` - **Variable Declaration**: This...

    解决C++全局变量只能初始化不能赋值的问题

    C++ requires a type specifier for all declarations 声明、初始化与赋值的区别: 声明:int a; 初始化:int a = 2;(在声明的时候顺带赋值叫做初始化) 赋值:a = 2; 只有定义(int a;)才分配存储空间,初始化...

    declarations.com.ua:我们的项目会数字化并打开所有乌克兰官员的声明

    clarifications.com.ua有用的链接设计: : 将扫描的声明数字化的表格: : 我们的众包框架将用于众包数字化流程: : 各种脚本来清理数据: : R生成分析的先决条件首先安装R 3.1或更高版本然后运行R并安装以下软件包...

Global site tag (gtag.js) - Google Analytics