Upgrade to Pro — share decks privately, control downloads, hide ads and more …

インフラエンジニアが教えるJSの型

tanishi
February 17, 2018

 インフラエンジニアが教えるJSの型

#kosen12s の資料

tanishi

February 17, 2018
Tweet

More Decks by tanishi

Other Decks in Programming

Transcript

  1. Reference number ECMA-123:2009 © Ecma International 2009 ECMA-262 8th Edition

    / June 2017 ECMAScript® 2017 Language Specification IUUQTXXXFDNBJOUFSOBUJPOBMPSHQVCMJDBUJPOTTUBOEBSET&DNBIUN
  2. NOTE The addition operator either performs string concatenation or numeric

    addition. AdditiveExpression : AdditiveExpression + MultiplicativeExpression 1. Let lref be the result of evaluating AdditiveExpression. 2. Let lval be ? GetValue(lref). 3. Let rref be the result of evaluating MultiplicativeExpression. 4. Let rval be ? GetValue(rref). 5. Let lprim be ? ToPrimitive(lval). 6. Let rprim be ? ToPrimitive(rval). 7. If Type(lprim) is String or Type(rprim) is String, then a. Let lstr be ? ToString(lprim). b. Let rstr be ? ToString(rprim). c. Return the String that is the result of concatenating lstr and rstr. 8. Let lnum be ? ToNumber(lprim). 9. Let rnum be ? ToNumber(rprim). 10. Return the result of applying the addition operation to lnum and rnum. See the Note below 12.8.5. NOTE 1 No hint is provided in the calls to ToPrimitive in steps 5 and 6. All standard objects except 12.8.3 The Addition Operator ( + ) 12.8.3.1 Runtime Semantics: Evaluation
  3. NOTE The addition operator either performs string concatenation or numeric

    addition. AdditiveExpression : AdditiveExpression + MultiplicativeExpression 1. Let lref be the result of evaluating AdditiveExpression. 2. Let lval be ? GetValue(lref). 3. Let rref be the result of evaluating MultiplicativeExpression. 4. Let rval be ? GetValue(rref). 5. Let lprim be ? ToPrimitive(lval). 6. Let rprim be ? ToPrimitive(rval). 7. If Type(lprim) is String or Type(rprim) is String, then a. Let lstr be ? ToString(lprim). b. Let rstr be ? ToString(rprim). c. Return the String that is the result of concatenating lstr and rstr. 8. Let lnum be ? ToNumber(lprim). 9. Let rnum be ? ToNumber(rprim). 10. Return the result of applying the addition operation to lnum and rnum. See the Note below 12.8.5. NOTE 1 No hint is provided in the calls to ToPrimitive in steps 5 and 6. All standard objects except 12.8.3 The Addition Operator ( + ) 12.8.3.1 Runtime Semantics: Evaluation
  4. NOTE The addition operator either performs string concatenation or numeric

    addition. AdditiveExpression : AdditiveExpression + MultiplicativeExpression 1. Let lref be the result of evaluating AdditiveExpression. 2. Let lval be ? GetValue(lref). 3. Let rref be the result of evaluating MultiplicativeExpression. 4. Let rval be ? GetValue(rref). 5. Let lprim be ? ToPrimitive(lval). 6. Let rprim be ? ToPrimitive(rval). 7. If Type(lprim) is String or Type(rprim) is String, then a. Let lstr be ? ToString(lprim). b. Let rstr be ? ToString(rprim). c. Return the String that is the result of concatenating lstr and rstr. 8. Let lnum be ? ToNumber(lprim). 9. Let rnum be ? ToNumber(rprim). 10. Return the result of applying the addition operation to lnum and rnum. See the Note below 12.8.5. NOTE 1 No hint is provided in the calls to ToPrimitive in steps 5 and 6. All standard objects except 12.8.3 The Addition Operator ( + ) 12.8.3.1 Runtime Semantics: Evaluation
  5. false. The following abstract operations are used in this speci

    ication to operate on references: 1. ReturnIfAbrupt(V). 2. If Type(V) is not Reference, return V. 3. Let base be GetBase(V). 4. If IsUnresolvableReference(V) is true, throw a ReferenceError exception. 5. If IsPropertyReference(V) is true, then a. If HasPrimitiveBase(V) is true, then i. Assert: In this case, base will never be unde ined or null. ii. Set base to ! ToObject(base). b. Return ? base.[[Get]](GetReferencedName(V), GetThisValue(V)). 6. Else base must be an Environment Record, a. Return ? base.GetBindingValue(GetReferencedName(V), IsStrictReference(V)) (see 8.1.1). NOTE The object that may be created in step 5.a.ii is not accessible outside of the above abstract operation and the ordinary object [[Get]] internal method. An implementation might 6.2.4.1 GetValue ( V )
  6. ECMAScriptͷܕ • The Undeined Type • The Null Type •

    The Boolean Type • The String Type • The Symbol Type • The Number Type • The Object Type
  7. false. The following abstract operations are used in this speci

    ication to operate on references: 1. ReturnIfAbrupt(V). 2. If Type(V) is not Reference, return V. 3. Let base be GetBase(V). 4. If IsUnresolvableReference(V) is true, throw a ReferenceError exception. 5. If IsPropertyReference(V) is true, then a. If HasPrimitiveBase(V) is true, then i. Assert: In this case, base will never be unde ined or null. ii. Set base to ! ToObject(base). b. Return ? base.[[Get]](GetReferencedName(V), GetThisValue(V)). 6. Else base must be an Environment Record, a. Return ? base.GetBindingValue(GetReferencedName(V), IsStrictReference(V)) (see 8.1.1). NOTE The object that may be created in step 5.a.ii is not accessible outside of the above abstract operation and the ordinary object [[Get]] internal method. An implementation might 6.2.4.1 GetValue ( V )
  8. NOTE The addition operator either performs string concatenation or numeric

    addition. AdditiveExpression : AdditiveExpression + MultiplicativeExpression 1. Let lref be the result of evaluating AdditiveExpression. 2. Let lval be ? GetValue(lref). 3. Let rref be the result of evaluating MultiplicativeExpression. 4. Let rval be ? GetValue(rref). 5. Let lprim be ? ToPrimitive(lval). 6. Let rprim be ? ToPrimitive(rval). 7. If Type(lprim) is String or Type(rprim) is String, then a. Let lstr be ? ToString(lprim). b. Let rstr be ? ToString(rprim). c. Return the String that is the result of concatenating lstr and rstr. 8. Let lnum be ? ToNumber(lprim). 9. Let rnum be ? ToNumber(rprim). 10. Return the result of applying the addition operation to lnum and rnum. See the Note below 12.8.5. NOTE 1 No hint is provided in the calls to ToPrimitive in steps 5 and 6. All standard objects except 12.8.3 The Addition Operator ( + ) 12.8.3.1 Runtime Semantics: Evaluation
  9. other speci ication types are used with these operations. The

    abstract operation ToPrimitive takes an input argument and an optional argument PreferredType. The abstract operation ToPrimitive converts its input argument to a non-Object type. If an object is capable of converting to more than one primitive type, it may use the optional hint PreferredType to favour that type. Conversion occurs according to the following algorithm: 1. Assert: input is an ECMAScript language value. 2. If Type(input) is Object, then a. If PreferredType was not passed, let hint be "default". b. Else if PreferredType is hint String, let hint be "string". c. Else PreferredType is hint Number, let hint be "number". 7.1.1 ToPrimitive ( input [ , PreferredType ] ) d. Let exoticToPrim be ? GetMethod(input, @@toPrimitive). e. If exoticToPrim is not unde ined, then i. Let result be ? Call(exoticToPrim, input, « hint »). ii. If Type(result) is not Object, return result. iii. Throw a TypeError exception. f. If hint is "default", set hint to "number". g. Return ? OrdinaryToPrimitive(input, hint). 3. Return input. NOTE When ToPrimitive is called with no hint, then it generally behaves as if the hint were
  10. NOTE The addition operator either performs string concatenation or numeric

    addition. AdditiveExpression : AdditiveExpression + MultiplicativeExpression 1. Let lref be the result of evaluating AdditiveExpression. 2. Let lval be ? GetValue(lref). 3. Let rref be the result of evaluating MultiplicativeExpression. 4. Let rval be ? GetValue(rref). 5. Let lprim be ? ToPrimitive(lval). 6. Let rprim be ? ToPrimitive(rval). 7. If Type(lprim) is String or Type(rprim) is String, then a. Let lstr be ? ToString(lprim). b. Let rstr be ? ToString(rprim). c. Return the String that is the result of concatenating lstr and rstr. 8. Let lnum be ? ToNumber(lprim). 9. Let rnum be ? ToNumber(rprim). 10. Return the result of applying the addition operation to lnum and rnum. See the Note below 12.8.5. NOTE 1 No hint is provided in the calls to ToPrimitive in steps 5 and 6. All standard objects except 12.8.3 The Addition Operator ( + ) 12.8.3.1 Runtime Semantics: Evaluation
  11. NOTE The unary + operator converts its operand to Number

    type. UnaryExpression : + UnaryExpression 1. Let expr be the result of evaluating UnaryExpression. 2. Return ? ToNumber(? GetValue(expr)). NOTE The unary - operator converts its operand to Number type and then negates it. Negating +0 produces -0, and negating -0 produces +0. 12.5.6 Unary + Operator 12.5.6.1 Runtime Semantics: Evaluation 12.5.7 Unary - Operator 12.5.7.1 Runtime Semantics: Evaluation
  12. NOTE The unary + operator converts its operand to Number

    type. UnaryExpression : + UnaryExpression 1. Let expr be the result of evaluating UnaryExpression. 2. Return ? ToNumber(? GetValue(expr)). NOTE The unary - operator converts its operand to Number type and then negates it. Negating +0 produces -0, and negating -0 produces +0. 12.5.6 Unary + Operator 12.5.6.1 Runtime Semantics: Evaluation 12.5.7 Unary - Operator 12.5.7.1 Runtime Semantics: Evaluation
  13. The abstract operation ToNumber converts argument to a value of

    type Number according to Table 10: Table 10: ToNumber Conversions Argument Type Result Unde ined Return NaN. Null Return +0. Boolean If argument is true, return 1. If argument is false, return +0. Number Return argument (no conversion). String See grammar and conversion algorithm below. Symbol Throw a TypeError exception. Object Apply the following steps: 1. Let primValue be ? ToPrimitive(argument, hint Number). 2. Return ? ToNumber(primValue). 7.1.3 ToNumber ( argument )
  14. Ԡ༻ const n = 12 console.log(n + ‘’) => ’12’

    const s = ’12’ console.log(+s) => 12
  15. JavaScriptͷίʔυͰ͢ [][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]][([][(![]+[])[+[]]+([! []]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]] +([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+ [])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+ []]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+ []+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]]((![]+[])[+!+[]]+(![]+[])[!+[]+!+[]]+(!![]+[]) [!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]+(!![]+[])[+[]]+(![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+ []+!+[]+!+[]]+(!![]+[])[+!+[]]])[!+[]+!+[]+[+[]]]+([]+[])[(![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!! []+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(!![]+[])[+[]]+([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+

    []]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+ []+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+ ([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]]()[+!+[] +[!+[]+!+[]]]+(+(!+[]+!+[]+[+[]]))[(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[]) [!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(+![]+([]+[])[([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+ (!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+ []]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+ [])[+[]]+([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+ []+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[]) [+!+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([![]]+[][[]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(+![]+[![]]+ ([]+[])[([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[] +!+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+ []]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(! []+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[] [[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]])[!+[]+!+[]+[+ []]]](!+[]+!+[]+[+!+[]])+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[]) [+!+[]]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[!+[]+!+[]+!+[]]+([][[]]+[])[+!+[]]+[+!+[]]+[!+[]+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+([]+[]) [(![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]) [+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(!![]+[])[+[]]+([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+ []+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+ []+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[]) [+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]]()[+!+[]+[!+[]+!+[]]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[] +[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[!+[]+!+[]+[+[]]])()