ECMA 入门

  • 1,592 views
Uploaded on

 

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
1,592
On Slideshare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
38
Comments
0
Likes
7

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. ECMAScript 262-3 @chenchengpro
  • 2. Why ECMAScript?
  • 3. History• ECMA 262-3 1999 (current)• ECMA 262-5 2009
  • 4. SPEC ~
  • 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. 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. 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. Structure Expression, Statement, Function, ... Type, Type Convention
  • 9. Definitions• Host Object ( )• Native Object ( )• Build-in Object ( )
  • 10. Definitions • Host Object ( ) • Native Object ( ) • Build-in Object ( )test XMLHttpRequest, String, var obj = {}
  • 11. Type• Undefined• Null• Boolean (Primitive)• String• Number• Object
  • 12. ObjectReadOnlyDontEnumDontDelete Internal
  • 13. [[Prototype]][[Class]][[Value]][[Get]] ( )[[Put]] ( , )[[CanPut]] ( )[[HasProperty]] ( )[[Delete]] ( )[[DefaultValue]] (hint) ( )[[Construct]] caller ( ) new[[Call]] caller ( )[[hasInstance]] ( ) ( )[[Scope]][[Match]] (String, Index)
  • 14. Type• Reference• List (Internal)• Completion
  • 15. Reference• • base • propertyName• delete operator• • identifier evaluate • property accessor
  • 16. Referencevar foo;function bar() {}//////////////////////fooReference = { base: global, propertyName: ‘foo’}barReference = { base: global, propertyName: ‘bar’}
  • 17. Referencefunction foo() { var bar;}//////////////////////FooBarReference = { base: foo.__scope__, // propertyName: ‘bar’}
  • 18. Referencevar foo = { bar: ‘’};//////////////////////FooBarReference = { base: foo, propertyName: ‘bar’}
  • 19. ReferenceGetBase(V) baseGetPropertyName(V) Reference VGetValue(V) V base V basePutValue(V, W) W
  • 20. Type Convention• ToPrimitive• ToBoolean• ToNumber, ToInteger• ToString• ToObject
  • 21. ToPrimitiveToPrimitive(Value, PreferredType) Undefined Null Boolean Number String Object [[DefaultValue]] Object PreferredType
  • 22. [[DefaultValue]][[DefaultValue]](hint)• hint String toString() valueOf()• hint Number valueOf(), toString()• hint Number (Date String)
  • 23. ToBooleanUndefined FALSE Null FALSEBooleanNumber +0, -0 NaN false, true (length 0) false String true Object TRUE
  • 24. ToNumberUndefined NaN Null 0Boolean true 1 false +0Number StringNumericLiteral String NaN ToPrimate(V, Number) Object ToNumber()
  • 25. ToStringUndefined “undefined” Null “null”Boolean “true” “false”Number ... String ToPrimate(V, String) Object ToString()
  • 26. ToObjectUndefined TypeError Null TypeErrorBoolean new Boolean()Number new Number() String new String() Object
  • 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. “equals operator”
  • 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. 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. 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. 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. == 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. // [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. “new operator”
  • 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. 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. • History• Type• Type Convention• Examples
  • 39. QA