es String 内部完成逻辑规范

String(value)

  1. 返回值范例是 string value
  2. 依据传入的值是不是为空字符串,选择性挪用 ToString(value) 要领

代码

function String(value) {
    return value === "" ? "" : ToString(value)
}

ToString(value)

ToString 是一个笼统的操纵,将传入的参数值转换为 String 范例的值,这一历程是参照一个划定规矩举行,划定规矩如下表

传入参数的范例返回的效果
Undefined“undefined”
Null“null”
Booleantrue => “true” false => “false”
String返回 参数值
Number参考下面细致论述
Object1. 挪用 ToPrimitive 要领,var primValue = ToPrimitive(input argument hintString); 2. 返回 ToString(primValue)

细致论述 Number 范例

Number 包含 NaN 特别的数值,还包含 Infinity 无穷大和无穷小的数值,包含能示意能存储下的一般浮点数

以下 m 替代传入的 Number 范例的参数的值

  • 当 m 为 NaN,返回 String 形的 “NaN”
  • 当 m 为 +0 或许 -0,返回 String 形的 “0”
  • 当 m < 0, 返回 -ToString(-m),意义是起首提掏出 – 标记,再对 -m 挪用 ToString 要领
  • 当 m 为 infinity,返回 String 形的 “Infinity”
  • 以下为重点,当 m > 0 且不是 infinity 时的内部笼统操纵

Otherwise, let n, k, and s be integers such that k ≥ 1, 10k−1 ≤ s < 10k, the Number value for s × 10n−k is m, and k is as small as possible. Note that k is the number of digits in the decimal representation of s, that s is not divisible by 10, and that the least significant digit of s is not necessarily uniquely determined by these criteria.

If k ≤ n ≤ 21, return the String consisting of the k digits of the
decimal representation of s (in order, with no leading zeroes),
followed by n−k occurrences of the character ‘0’.

If 0 < n ≤ 21, return the String consisting of the most significant n
digits of the decimal representation of s, followed by a decimal point
‘.’, followed by the remaining k−n digits of the decimal
representation of s.

If −6 < n ≤ 0, return the String consisting of the character ‘0’,
followed by a decimal point ‘.’, followed by −n occurrences of the
character ‘0’, followed by the k digits of the decimal representation
of s.

Otherwise, if k = 1, return the String consisting of the single digit
of s, followed by lowercase character ‘e’, followed by a plus sign ‘+’
or minus sign ‘−’ according to whether n−1 is positive or negative,
followed by the decimal representation of the integer abs(n−1) (with
no leading zeros).

Return the String consisting of the most significant digit of the
decimal representation of s, followed by a decimal point ‘.’, followed
by the remaining k−1 digits of the decimal representation of s,
followed by the lowercase character ‘e’, followed by a plus sign ‘+’
or minus sign ‘−’ according to whether n−1 is positive or negative,
followed by the decimal representation of the integer abs(n−1) (with
no leading zeros).

浅显的明白就是 科学计数法示意

参考
http://es5.github.io/#x15.5.1.1
http://es5.github.io/#x9.8

    原文作者:_ipo
    原文地址: https://segmentfault.com/a/1190000013407915
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞