ECMA 入门
-
Upload
chencheng- -
Category
Technology
-
view
1.644 -
download
10
Transcript of ECMA 入门
ECMAScript 262-3 入门云谦 @chenchengpro
11年1月5日星期三
Why ECMAScript?
11年1月5日星期三
History
• ECMA 262-3:1999 (current)
• ECMA 262-5:2009
11年1月5日星期三
SPEC ~
11年1月5日星期三
1. Scope
2. Conformance
3. References
4. Overview (概览)
5. Notational Conventions (标记约定)
6. Source Text (源码)
7. Lexical Conventions (词法约定)
8. Types (类型)
9. Type Convertion (类型转换)
10. Execution Contexts (执行上下文)
11. Expressions (表达式)
12. Statements (语句)
13. Function Definition (函数定义)
14. Program
15. Native ECMAScript Objects (ECMAScript 原生对象)
16. Errors (错误)
11年1月5日星期三
1. Scope
2. Conformance
3. References
4. Overview (概览)
5. Notational Conventions (标记约定)
6. Source Text (源码)
7. Lexical Conventions (词法约定)
8. Types (类型)
9. Type Convertion (类型转换)
10. Execution Contexts (执行上下文)
11. Expressions (表达式)
12. Statements (语句)
13. Function Definition (函数定义)
14. Program
15. Native ECMAScript Objects (ECMAScript 原生对象)
16. Errors (错误)
11年1月5日星期三
1. Scope
2. Conformance
3. References
4. Overview (概览)
5. Notational Conventions (标记约定)
6. Source Text (源码)
7. Lexical Conventions (词法约定)
8. Types (类型)
9. Type Convertion (类型转换)
10. Execution Contexts (执行上下文)
11. Expressions (表达式)
12. Statements (语句)
13. Function Definition (函数定义)
14. Program
15. Native ECMAScript Objects (ECMAScript 原生对象)
16. Errors (错误)
11年1月5日星期三
Structure
Type, Type Convention
Expression, Statement, Function, ...
11年1月5日星期三
Definitions
• Host Object (宿主对象)
• Native Object (原生对象)
• Build-in Object (内置对象)
11年1月5日星期三
Definitions
• Host Object (宿主对象)
• Native Object (原生对象)
• Build-in Object (内置对象)
test:XMLHttpRequest, String, var obj = {}
11年1月5日星期三
Type
• Undefined
• Null
• Boolean
• String
• Number
• Object
原始类型 (Primitive)
11年1月5日星期三
Object
ReadOnly 只读
DontEnum 不可枚举
DontDelete 不可删除
Internal 内部属性
11年1月5日星期三
[[Prototype]] 无 对象原型[[Class]] 无 对象类型[[Value]] 无 获取值[[Get]] (属性名) 获取属性值[[Put]] (属性名, 值) 设置属性值[[CanPut]] (属性名) 是否可设置[[HasProperty]] (属性名) 属性是否存在[[Delete]] (属性名) 删除属性[[DefaultValue]] (hint) 返回对象默认值(原始类型)
[[Construct]] caller 的参数 (构造器) 通过 new 构造对象[[Call]] caller 的参数 (函数) 执行相关代码[[hasInstance]] (值) 值是否为(函数)对象的实例[[Scope]] 无 函数执行时的作用域链[[Match]] (String, Index) 匹配正则表达式并返回匹配结果
11年1月5日星期三
Type
• Reference
• List
• Completion
内部类型 (Internal)
11年1月5日星期三
Reference
• 包含两部分• base
• propertyName
• 用途举例:delete operator
• 来源• identifier evaluate
• property accessor
11年1月5日星期三
Reference
var foo;function bar() {}
//////////////////////
fooReference = { base: global, propertyName: ‘foo’}barReference = { base: global, propertyName: ‘bar’}
11年1月5日星期三
Reference
function foo() { var bar;}
//////////////////////
FooBarReference = { base: foo.__scope__, // 在作用域链中往上查找 propertyName: ‘bar’}
11年1月5日星期三
Reference
var foo = { bar: ‘’};
//////////////////////
FooBarReference = { base: foo, propertyName: ‘bar’}
11年1月5日星期三
Reference
GetBase(V) 获取 base 对象
GetPropertyName(V) 获取属性名
GetValue(V)如不是 Reference,直接返回 V;否则获取 V 的 base 对象里对应的属性值
PutValue(V, W)将 V 的 base 对象里对应的属性设置为 W
11年1月5日星期三
Type Convention
• ToPrimitive
• ToBoolean
• ToNumber, ToInteger
• ToString
• ToObject
11年1月5日星期三
ToPrimitive
Undefined
不转换Null
不转换Boolean 不转换Number
不转换
String
不转换
Object调用 Object 的 [[DefaultValue]] 方法,并传入 PreferredType 参数
ToPrimitive(Value, PreferredType)
11年1月5日星期三
[[DefaultValue]]
• 如果 hint 为 String,先 toString(),后 valueOf()
• 如果 hint 为 Number,先 valueOf(), 后 toString()
• 如果 hint 没有传入,默认为 Number (Date 对象例外,为 String)
[[DefaultValue]](hint)
11年1月5日星期三
ToBoolean
Undefined FALSE
Null FALSE
Boolean 不转换
Number +0, -0 或 NaN 时为 false, 否则为 true
String空字符串时(length为0)为 false,否则
为 true
Object TRUE
11年1月5日星期三
ToNumber
Undefined NaN
Null 0
Boolean true 时为 1,false 时为 +0
Number 不转换
String用 StringNumericLiteral 语法解释,
不能解释的返回 NaN
Object先调用 ToPrimate(V, Number),再对
结果调用 ToNumber()
11年1月5日星期三
ToString
Undefined “undefined”
Null “null”
Boolean “true” 或 “false”
Number ...
String 不转换
Object先调用 ToPrimate(V, String),再对结
果调用 ToString()
11年1月5日星期三
ToObject
Undefined 报 TypeError
Null 报 TypeError
Boolean new Boolean()
Number new Number()
String new String()
Object 不转换
11年1月5日星期三
'' == '0' // ?
0 == '' // ?
0 == '0' // ?
false == 'false' // ?
false == '0' // ?
false == undefined // ?
null == undefined // ?
' \t\r\n ' == 0 // ?
[] == 0 // ?
var obj = {};
obj.valueOf=function(){return 1};
obj.toString=function(){return 0;}
obj == 1; // ?
11年1月5日星期三
搜 “equals operator”
11年1月5日星期三
11.9.1 The Equals Operator (== )
The production EqualityExpression : EqualityExpression == RelationalExpression is evaluated as follows:
1. 求值 EqualityExpression
2. 调用 GetValue(Result(1))
3. 求值 RelationalExpression
4. 调用 GetValue(Result(3))
5. 执行 Result(4) == Result(2) 的比较。(详见 11.9.3)。
6. 返回 Result(5).
11年1月5日星期三
11.9.3 The Abstract Equality Comparison Algorithm
The comparison x == y, where x and y are values, produces true or false. Such a comparison is performed as follows:
1. 如 Type(x) 不同于 Type(y),跳到步骤 14
2. 如 Type(x) 为 Undefined, 返回 true
3. 如 Type(x) 为 Null, 返回 true
4. 如 Type(x) 不是 Number, 跳到步骤 11
5. 如 x 为 NaN, 返回 false
6. 如 y 为 NaN, 返回 false
7. 如 x 和 y 的数值相同, 返回 true
8. 如 x 为 +0 且 y 为 -0, 返回 true
9. 如 x is -0 且 y 为 +0, 返回 true
10. 返回 false
同类型且Type(x) 为Number
11年1月5日星期三
11. 如 Type(x) 为 String, 仅当每个字符完全一致时返回 true;否则返回 false
12. 如 Type(x) 为 Boolean, 如 x 和 y 同为 true 或 同为 false,返回 true;否则返回 false
13. 如 x 和 y 指向相同对象或指向的对象之间有连接 (详见 13.1.2); 否则返回 false
同类型且Type(x) 不为Number
11年1月5日星期三
14. 如 x 为 null 且 y 为 undefined, 返回 true.
15. 如 x 为 undefined 且 y 为 null, 返回 true.
16. 如 Type(x) 为 Number 且 Type(y) 为 String, 返回 x == ToNumber(y) 的比较结果
17. 如 Type(x) 为 String 且 Type(y) 为 Number, 返回 ToNumber(x)== y 的比较结果
18. 如 Type(x) 为 Boolean, 返回 ToNumber(x)== y 的比较结果
19. 如 Type(y) 为 Boolean, 返回 x == ToNumber(y) 的比较结果
20. 如 Type(x) 为 String 或 Number 且 Type(y) 为 Object, 返回 x == ToPrimitive(y) 的比较结果
21. 如 Type(x) 为 Object 且 Type(y) 为 String 或 Number, 返回 ToPrimitive(x)== y 的比较结果
22. Return false.
11年1月5日星期三
'' == '0' // ?
0 == '' // ?
0 == '0' // ?
false == 'false' // ?
false == '0' // ?
false == undefined // ?
null == undefined // ?
' \t\r\n ' == 0 // ?
[] == 0 // ?
var obj = {};
obj.valueOf=function(){return 1};
obj.toString=function(){return 0;}
obj == 1; // ?
11年1月5日星期三
// [Object Object]
alert(new function() {return "圆心"});
// “圆心”alert(new function() {return new String(“圆心”);});
// Why?
ref: http://www.planabc.net/2008/02/20/javascript_new_function/
11年1月5日星期三
搜 “new operator”
11年1月5日星期三
11.2.2 The new Operator
The production NewExpression : new NewExpression is evaluated as follows:
1. 求值 NewExpression
2. 调用 GetValue(Result(1))
3. 如 Type(Result(2)) 不是 Object, 抛出 TypeError 异常
4. 如 Result(2) 没有实现内部方法 [[Construct]], 抛出 TypeError 异常
5. 调用 Result(2) 的 [[Construct]] 方法
6. 返回 Result(5)
11年1月5日星期三
13.2.2 [[Construct]]
When the [[Construct]] property for a Function object F is called, the following steps are taken:
1. 创建一个新的原生对象2. 设置 Result(1) 的 [[Class]] 属性为 "Object"
3. 获取 F 的 prototype 属性4. 如 Result(3) 为 object , 把 Result(1) 的 [[Prototype]] 属性设为 Result(3)
5. 如 Result(3) 不为 object , 把 Result(1) 的 [[Prototype]] 属性设为原始 Object 的 prototype 对象 (详见 15.2.3.1)
6. 调用 F 的 [[Call]] 属性,Result(1) 为 this 值且传入 [[Construct]] 的参数7. 如 Type(Result(6)) 为 Object 则返回 Result(6)
8. 返回 Result(1)
11年1月5日星期三
回顾
• History
• Type
• Type Convention
• Examples
11年1月5日星期三
QA
11年1月5日星期三