资讯专栏INFORMATION COLUMN

es String 内部实现逻辑标准

Jingbin_ / 3002人阅读

摘要:返回值类型是根据传入的值是否为空字符串,选择性调用方法代码是一个抽象的操作,将传入的参数值转换为类型的值,这一过程是参照一个规则进行,规则如下表传入参数的类型返回的结果返回参数值参考下面详细阐述调用方法,返回详细阐述类型包括特殊的数值,还包

String(value)

返回值类型是 string value

根据传入的值是否为空字符串,选择性调用 ToString(value) 方法

代码

function String(value) {
    return value === "" ? "" : ToString(value)
}
ToString(value)
ToString 是一个抽象的操作,将传入的参数值转换为 String 类型的值,这一过程是参照一个规则进行,规则如下表
传入参数的类型 返回的结果
Undefined "undefined"
Null "null"
Boolean true => "true" false => "false"
String 返回 参数值
Number 参考下面详细阐述
Object 1. 调用 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

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/92959.html

相关文章

  • ToPrimitive 内部逻辑标准

    摘要:是包含两个参数的抽象的操作,一个是参数,一个是可选参数,该操作就是将转换为非类型的值,即数据类型的种普通类型。在第一个参数为时,有可能将该转换不止一种基本类型的值。 ToPrimitive 是包含两个参数的抽象的操作,一个是 input argument 参数,一个是可选参数 PreferredType,该操作就是将 input argument 转换为 no-Object type ...

    chanthuang 评论0 收藏0
  • Zepto 源码分析 2 - Polyfill 设计

    摘要:此模块包含的设计思路即为预以匹配降级方案。没有默认编译该模块,以及利用该模块判断后提供平台相关逻辑的主要原因在于其设计原则的代码完成核心的功能。此处,也引出了代码实现的另一个基本原则面向功能标准,先功能覆盖再优雅降级。 在进入 Zepto Core 模块代码之前,本节简略列举 Zepto 及其他开源库中一些 Polyfill 的设计思路与实现技巧。 涉及模块:IE/IOS 3/Dete...

    chuyao 评论0 收藏0
  • JavaScript是如何工作的:深入类和继承内部原理+Babel和 TypeScript 之间转换

    摘要:下面是用实现转成抽象语法树如下还支持继承以下是转换结果最终的结果还是代码,其中包含库中的一些函数。可以使用新的易于使用的类定义,但是它仍然会创建构造函数和分配原型。 这是专门探索 JavaScript 及其所构建的组件的系列文章的第 15 篇。 想阅读更多优质文章请猛戳GitHub博客,一年百来篇优质文章等着你! 如果你错过了前面的章节,可以在这里找到它们: JavaScript 是...

    PrototypeZ 评论0 收藏0
  • ECMAScript 2018 标准导读

    摘要:标准对象,语义由本规范定义的对象。三个冒号作为分隔符分割数字字符串文法的产生式。所有因为带来的问题,基本上是占着茅坑不拉屎的行为导致。以数组测试操作为例,标准中的描述如下相对于来说,规范中增加了对的处理。 前言 本文是对《ECMAScript 2018 Language Specification》的解读。本文是对标准的概述性导读,不是对 ES2018特性的详细描述,也不会针对某个技术...

    MiracleWong 评论0 收藏0
  • ES6 Symbol - 一些重要的Symbol属性

    摘要:是一个布尔值,用于确定当调用数组的方法时,如果传入参数是一个数组,是否需要将这个数组拍平。与其他的属性不同的是,并不默认出现在标准对象中。 ECMAScript 6 通过在原型链上定义与Symbol相关的属性来暴露语言内部逻辑,使得开发者可以对一些语言的默认行为做配置。接下来我们来看看有哪些重要的Symbol属性可供我们使用: 1: Symbol.hasInstance 一个在执行 i...

    Barrior 评论0 收藏0

发表评论

0条评论

Jingbin_

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<