[JavaScript]WTRF JS

7261 ワード

1.Non-coercible object
function nonCoercible(val) {
  if (val == null) {
    throw TypeError('nonCoercible should not be called with null or undefined')
  }

  const res = Object(val)

  res[Symbol.toPrimitive] = () => {
    throw TypeError('Trying to coerce non-coercible object')
  }

  return res
}
// objects
const foo = nonCoercible({foo: 'foo'})

foo * 10      // -> TypeError: Trying to coerce non-coercible object
foo + 'evil'  // -> TypeError: Trying to coerce non-coercible object

// strings
const bar = nonCoercible('bar')

bar + '1'                 // -> TypeError: Trying to coerce non-coercible object
bar.toString() + 1        // -> bar1
bar === 'bar'             // -> false
bar.toString() === 'bar'  // -> true
bar == 'bar'              // -> TypeError: Trying to coerce non-coercible object

// numbers
const baz = nonCoercible(1)

baz == 1             // -> TypeError: Trying to coerce non-coercible object
baz === 1            // -> false
baz.valueOf() === 1  // -> true
ToPrimitive(input[,PrferredType])
  • Asssert:input is an ECMAScript laguage value.
  • If Type(input)is Object,then 2.a If PrferredType was not passed,let hint be「default」.2.b Else if PrferredTypt String,let hint be「string」.2.c Else Prrrextredypt Prreexypt.Nypt.ine Prexypt.ine Prexypt.ine Prexypt.Nypt.ine Prexypt.Nypt.Get Method(input、@toPrimitive).2.e If exotic ToPrim is not undefined,then 2.e.(1)Let reult be?Call(exoticToPrim,input,「hint」).2.e.(2)If Type is not Object,return reult.2.e.(3)Throw a Typerr exception.2 f If hint is「default」,set hint「Rorn.」Ordinary ToPrimitive.
  • Return input.
  • Ordinary ToPrimitive(O,hint)
  • Asssert:Type(O)is Object.
  • Asssert:Type is String and its value is eigther「string」or「number」
  • If hint is「string」,then 3.a Let methodNames be「toString」,「valueOf」.
  • Else,4.a Let methodNames be≪valueOf≫「toString」}
  • For each name in methodNames in List order,do 5 a Let method be?Get(O,name).5.b If IsCallable(method)is true,then 5 b.(1)Let reult be?Call(method,O).5.b.(2)If Type(reult)is not Object,return reult.
  • Throw a TypeErr exception.
  • 6.1.5.1 Well-Known Symbros 19.4.2.12 Symbol.toPrimitive
    7.1.1 ToPrimitive(input[,PrferredType])7.1.3 ToNumber(argment)7.1.12 ToStering(argment)
    2.try…finally
    (() => {
      try {
        return 2;
      } finally {
        return 3;
      }
    })();    // 3
    
    TrySttement:try Block Catch Finally
  • Let B be the result of evaluating Block.
  • If B.[Type]is throw,let C be CatClause Everaluation of Catch with parameter B.[Value].
  • Else,let C be B.
  • Let F be the result of evaluating Finally.
  • If F.[Type]is normal,set F to C.
  • Return Compration(UpdateEmpty(F,undefined).
  • 13.15.8 Runtime Semantics:Everaluation
    3.Labelled statements
    foo: {
      console.log('first');
      break foo;
      console.log('second');
    }    // first
    
    foo:{
      while(true){
        break foo;
      }
      console.log(1);
    }
    console.log(2);    // 2
    
    A Sttement may be prefixed by a label.Labelled statemens are only used in conjunction with labelled break and conting statements.ECMAScript hasのgoto statement.
    13.13 Labelled Sttements
    4.Arthmetics
    3  - 1  // -> 2
     3  + 1  // -> 4
    '3' - 1  // -> 2
    '3' + 1  // -> '31'
    
    '' + '' // -> ''
    [] + [] // -> ''
    {} + [] // -> 0
    [] + {} // -> '[object Object]'
    {} + {} // -> '[object Object][object Object]'
    
    '222' - -'111' // -> 333
    
    [4] * [4]       // -> 16
    [] * []         // -> 0
    [4, 4] * [4, 4] // NaN
    
    Additive Expression:Additive Expression+MultilicativeExpression
  • Let lref be the relt of evaluating Additive Expression.
  • Let lval be?GetValue(lref).
  • Let rref be the result of evaluating MulticativeExprestion.
  • Let rval be?GetValue(rref).
  • Let lprem be?ToPrimitive(lval).
  • Let rprim be?ToPrimitive(rval).
  • If Type(lprm)is String or Type(rprm)is String,then 7 a Let lstr be?ToString.7 b Let rstr be?ToString.7.c Return the String that is the result of concatenting lstr.
  • Let lnum be?ToNumber.
  • Let rnum be?ToNumber.
  • Return the result of applying the addition operation to lnum and rnum.
  • 12.8.3 The Addition Operator(+)
    7.1.1 ToPrimitive(input[,PrferredType])7.1.12 ToStering(argment)7.1.3 ToNumber(argment)
    5.Numbers
    999999999999999  // -> 999999999999999
    9999999999999999 // -> 10000000000000000
    
    10000000000000000       // -> 10000000000000000
    10000000000000000 + 1   // -> 10000000000000000
    10000000000000000 + 1.1 // -> 10000000000000002
    
    0.1 + 0.2 // -> 0.30000000000000004
    
    6.1.6 The Number Type 0.3000万0004.com
    6.HTML comments
    // valid comment
    
    <div id=「right-1」class=「col-lg-12 col-sm-4 col-xs-4 ad」


    <div id=「right-2」class=「col-lg-12 col-sm-4 col-xs-4 ad」


    <div id=「right-3」class=「col-lg-12 col-sm-4 col-xs-4 ad」