惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

阮一峰的网络日志
阮一峰的网络日志
Last Week in AI
Last Week in AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
U
Unit 42
J
Java Code Geeks
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
罗磊的独立博客
月光博客
月光博客
腾讯CDC
Stack Overflow Blog
Stack Overflow Blog
小众软件
小众软件
B
Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
美团技术团队
Y
Y Combinator Blog
T
Tailwind CSS Blog
宝玉的分享
宝玉的分享
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园_首页
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
爱范儿
爱范儿
B
Blog RSS Feed
V
Visual Studio Blog
MyScale Blog
MyScale Blog

博客园 - microsheen

[MDX学习笔记之五]优化Set操作——SUM中的CrossJoin [MDX学习笔记之四]Where vs. Subselect/Subcube [MDX学习笔记之三]MDX的上下文(Context) [MDX学习笔记之二]在MDX中处理边界情况 SQL Server 2005 SP2 CTP 在11月7号发布了 [MDX学习笔记之一]MDX中一些常见的计算 SSAS中Cube的结构 如何使用MDX编写同比和环比 SQL Server2005对XML的支持 2006 Tech.Ed 第二天见闻 2006 Tech.Ed 第一天见闻 C#简单命名指南 [.net泛型学习笔记之三]再测泛型的性能 [.net泛型学习笔记之二]泛型的性能 [.net泛型学习笔记之一]泛型介绍 [设计模式学习笔记之二]设计模式和量体剪衣 Note Three: studying JavaScript: The Definitive Guide, 4th Edition Note One —— studying JavaScript: The Definitive Guide, 4th Edition [设计模式学习笔记之一]面向对象是什么?
Note Two : studying JavaScript: The Definitive Guide, 4th...
microsheen · 2006-02-11 · via 博客园 - microsheen

Chapter 4. Variables
1. Variable Declaration

please test the followings in browser.

<script language="javascript">
var a;
e = "I have a value";

//If you don't specify an initial value for a variable with the var statement,
//the variable is declared, but its initial value is undefined until your code stores a value into it.
document.write("a = ", a, "<br />"); 

//If you assign a value to a variable that you have not declared with var,
//JavaScript will implicitly declare that variable for you.
//however, that implicitly declared variables are always created as global variables,
//even if they are used within the body of a function.
document.write("e = ", e, "<br />"); 

//If you attempt to read the value of an undeclared variable, JavaScript will generate an error
//document.write("f = ", f, "<br />"); //it will throw an exception if you run it.
</script>

the result shoule be:
a = undefined
e = I have a value

2. Variable Scope
1)JavaScript will implicitly declare that variable for you.  and that implicitly declared variables are always created as global variables,
2) JavaScript does not have block-level scope. All variables declared in a function, no matter where they are declared, are defined throughout the function.
but this rule can cause surprising results. The following code illustrates this:

var scope = "global";
function f(  ) {
    alert(scope);         // Displays "undefined", not "global"
    var scope = "local";  // Variable initialized here, but defined everywhere
    alert(scope);         // Displays "local"
}
f(  );

3. Primitive Types and Reference Types
1) Numbers, boolean, null, undefined  
these types have a fixed size in memory. the variable can directly hold any primitive value

2) Strings
strings are immutable: there is no way to change the contents of a string value. strings as an immutable reference type that behaves like a primitive type or as a primitive type implemented with the internal efficiency of a reference type.

3) reference types
they do not have a fixed size. the variable stores a reference to the value. Typically, this reference is some form of pointer or memory address.

4. Variables as Properties
Variables in JavaScript are fundamentally the same as object properties.
1) The Global Object
before executing any JavaScript code, is create a global object. The properties of this object are the global variables of JavaScript programs.

In top-level code (i.e., JavaScript code that is not part of a function), you can use the JavaScript keyword this to refer to the global object.

In client-side JavaScript, the Window object serves as the global object for all JavaScript code contained in the browser window it represents.

2) Local Variables
While the body of a function is executing, the function arguments and local variables are stored as properties of this call object

3) JavaScript Execution Contexts
Each time the JavaScript interpreter begins to execute a function, it creates a new execution context for that function.

JavaScript code that is not part of any function runs in an execution context that uses the global object for variable definitions. And every JavaScript function runs in its own unique execution context with its own call object in which local variables are defined.

5. Variable Scope Revisited
1) In top-level JavaScript code (i.e., code not contained within any function definitions), the scope chain consists of a single object
2) In a (non-nested) function, however, the scope chain consists of two objects. The first is the function's call object, and the second is the global object.
3)A nested function would have three or more objects in its scope chain.