ECMAScript 262-3        @chenchengpro
Why ECMAScript?
History• ECMA 262-3   1999 (current)• ECMA 262-5   2009
SPEC ~
1. Scope2. Conformance3. References4. Overview (      )5. Notational Conventions (        )6. Source Text (     )7. Lexica...
1. Scope2. Conformance3. References4. Overview (      )5. Notational Conventions (        )6. Source Text (     )7. Lexica...
1. Scope2. Conformance3. References4. Overview (      )5. Notational Conventions (        )6. Source Text (     )7. Lexica...
Structure Expression, Statement, Function, ...    Type, Type Convention
Definitions• Host Object (       )• Native Object (         )• Build-in Object (           )
Definitions • Host Object (         ) • Native Object (           ) • Build-in Object (             )test XMLHttpRequest, S...
Type• Undefined• Null• Boolean           (Primitive)• String• Number• Object
ObjectReadOnlyDontEnumDontDelete Internal
[[Prototype]][[Class]][[Value]][[Get]]           (        )[[Put]]           (        ,   )[[CanPut]]        (        )[[H...
Type• Reference• List                (Internal)• Completion
Reference•    • base    • propertyName•             delete operator•    • identifier evaluate    • property accessor
Referencevar foo;function bar() {}//////////////////////fooReference = {    base: global,    propertyName: ‘foo’}barRefere...
Referencefunction foo() {    var bar;}//////////////////////FooBarReference = {    base: foo.__scope__, //    propertyName...
Referencevar foo = {    bar: ‘’};//////////////////////FooBarReference = {    base: foo,    propertyName: ‘bar’}
ReferenceGetBase(V)                   baseGetPropertyName(V)                               Reference   VGetValue(V)       ...
Type Convention• ToPrimitive• ToBoolean• ToNumber, ToInteger• ToString• ToObject
ToPrimitiveToPrimitive(Value, PreferredType)  Undefined    Null   Boolean   Number    String                  Object     [[...
[[DefaultValue]][[DefaultValue]](hint)•        hint    String     toString()    valueOf()•         hint   Number        va...
ToBooleanUndefined                  FALSE  Null                    FALSEBooleanNumber     +0, -0   NaN      false,         ...
ToNumberUndefined                NaN  Null                     0Boolean      true      1   false     +0Number            St...
ToStringUndefined          “undefined”  Null               “null”Boolean        “true”         “false”Number                ...
ToObjectUndefined          TypeError  Null            TypeErrorBoolean        new Boolean()Number         new Number() Stri...
== 0 // ?0 ==  // ?0 == 0 // ?false == false // ?false == 0 // ?false == undefined // ?null == undefined // ? trn  == 0 //...
“equals operator”
11.9.1 The Equals Operator (== )The production EqualityExpression : EqualityExpression ==RelationalExpression is evaluated...
11.9.3 The Abstract Equality Comparison AlgorithmThe comparison x == y, where x and y are values, produces true orfalse. S...
11.   Type(x)     String,          true               false12.   Type(x)     Boolean,     x     y         true        fals...
14.    x    null     y       undefined,         true.15.    x    undefined          y      null,     true.16. Type(x)       ...
== 0 // ?0 ==  // ?0 == 0 // ?false == false // ?false == 0 // ?false == undefined // ?null == undefined // ? trn  == 0 //...
// [Object Object]     alert(new function() {return "                           "});     // “        ”     alert(new funct...
“new operator”
11.2.2 The new OperatorThe production NewExpression : new NewExpression is evaluated asfollows:1.     NewExpression2.     ...
13.2.2 [[Construct]]When the [[Construct]] property for a Function object F is called,the following steps are taken:1.2.  ...
• History• Type• Type Convention• Examples
QA
Upcoming SlideShare
Loading in …5
×

ECMA 入门

1,905 views

Published on

Published in: Technology
0 Comments
8 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,905
On SlideShare
0
From Embeds
0
Number of Embeds
365
Actions
Shares
0
Downloads
42
Comments
0
Likes
8
Embeds 0
No embeds

No notes for slide

ECMA 入门

  1. 1. ECMAScript 262-3 @chenchengpro
  2. 2. Why ECMAScript?
  3. 3. History• ECMA 262-3 1999 (current)• ECMA 262-5 2009
  4. 4. SPEC ~
  5. 5. 1. Scope2. Conformance3. References4. 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. Program15. Native ECMAScript Objects (ECMAScript )16. Errors ( )
  6. 6. 1. Scope2. Conformance3. References4. 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. Program15. Native ECMAScript Objects (ECMAScript )16. Errors ( )
  7. 7. 1. Scope2. Conformance3. References4. 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. Program15. Native ECMAScript Objects (ECMAScript )16. Errors ( )
  8. 8. Structure Expression, Statement, Function, ... Type, Type Convention
  9. 9. Definitions• Host Object ( )• Native Object ( )• Build-in Object ( )
  10. 10. Definitions • Host Object ( ) • Native Object ( ) • Build-in Object ( )test XMLHttpRequest, String, var obj = {}
  11. 11. Type• Undefined• Null• Boolean (Primitive)• String• Number• Object
  12. 12. ObjectReadOnlyDontEnumDontDelete Internal
  13. 13. [[Prototype]][[Class]][[Value]][[Get]] ( )[[Put]] ( , )[[CanPut]] ( )[[HasProperty]] ( )[[Delete]] ( )[[DefaultValue]] (hint) ( )[[Construct]] caller ( ) new[[Call]] caller ( )[[hasInstance]] ( ) ( )[[Scope]][[Match]] (String, Index)
  14. 14. Type• Reference• List (Internal)• Completion
  15. 15. Reference• • base • propertyName• delete operator• • identifier evaluate • property accessor
  16. 16. Referencevar foo;function bar() {}//////////////////////fooReference = { base: global, propertyName: ‘foo’}barReference = { base: global, propertyName: ‘bar’}
  17. 17. Referencefunction foo() { var bar;}//////////////////////FooBarReference = { base: foo.__scope__, // propertyName: ‘bar’}
  18. 18. Referencevar foo = { bar: ‘’};//////////////////////FooBarReference = { base: foo, propertyName: ‘bar’}
  19. 19. ReferenceGetBase(V) baseGetPropertyName(V) Reference VGetValue(V) V base V basePutValue(V, W) W
  20. 20. Type Convention• ToPrimitive• ToBoolean• ToNumber, ToInteger• ToString• ToObject
  21. 21. ToPrimitiveToPrimitive(Value, PreferredType) Undefined Null Boolean Number String Object [[DefaultValue]] Object PreferredType
  22. 22. [[DefaultValue]][[DefaultValue]](hint)• hint String toString() valueOf()• hint Number valueOf(), toString()• hint Number (Date String)
  23. 23. ToBooleanUndefined FALSE Null FALSEBooleanNumber +0, -0 NaN false, true (length 0) false String true Object TRUE
  24. 24. ToNumberUndefined NaN Null 0Boolean true 1 false +0Number StringNumericLiteral String NaN ToPrimate(V, Number) Object ToNumber()
  25. 25. ToStringUndefined “undefined” Null “null”Boolean “true” “false”Number ... String ToPrimate(V, String) Object ToString()
  26. 26. ToObjectUndefined TypeError Null TypeErrorBoolean new Boolean()Number new Number() String new String() Object
  27. 27. == 0 // ?0 == // ?0 == 0 // ?false == false // ?false == 0 // ?false == undefined // ?null == undefined // ? trn == 0 // ?[] == 0 // ?var obj = {};obj.valueOf=function(){return 1};obj.toString=function(){return 0;}obj == 1; // ?
  28. 28. “equals operator”
  29. 29. 11.9.1 The Equals Operator (== )The production EqualityExpression : EqualityExpression ==RelationalExpression is evaluated as follows:1. EqualityExpression2. GetValue(Result(1))3. RelationalExpression4. GetValue(Result(3))5. Result(4) == Result(2) ( 11.9.3)6. Result(5).
  30. 30. 11.9.3 The Abstract Equality Comparison AlgorithmThe comparison x == y, where x and y are values, produces true orfalse. Such a comparison is performed as follows:1. Type(x) Type(y) 142. Type(x) Undefined, true3. Type(x) Null, true4. Type(x) Number, 115. x NaN, false6. y NaN, false7. x y , true Type(x)8. x +0 y -0, true Number9. x is -0 y +0, true10. false
  31. 31. 11. Type(x) String, true false12. Type(x) Boolean, x y true false true false Type(x)13. x y Number ( 13.1.2) false
  32. 32. 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)== y18. Type(x) Boolean, ToNumber(x)== y19. 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)== y22. Return false.
  33. 33. == 0 // ?0 == // ?0 == 0 // ?false == false // ?false == 0 // ?false == undefined // ?null == undefined // ? trn == 0 // ?[] == 0 // ?var obj = {};obj.valueOf=function(){return 1};obj.toString=function(){return 0;}obj == 1; // ?
  34. 34. // [Object Object] alert(new function() {return " "}); // “ ” alert(new function() {return new String(“ ”);}); // Why?ref: http://www.planabc.net/2008/02/20/javascript_new_function/
  35. 35. “new operator”
  36. 36. 11.2.2 The new OperatorThe production NewExpression : new NewExpression is evaluated asfollows:1. NewExpression2. GetValue(Result(1))3. Type(Result(2)) Object, TypeError4. Result(2) [[Construct]], TypeError5. Result(2) [[Construct]]6. Result(5)
  37. 37. 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 prototype4. 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)
  38. 38. • History• Type• Type Convention• Examples
  39. 39. QA

×