javascript预定义函数(parseInt,parseFloat,isNaN等)的算法 [英] Algorithm for javascript pre-defined functions (parseInt, parseFloat, isNaN, etc.)

查看:58
本文介绍了javascript预定义函数(parseInt,parseFloat,isNaN等)的算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看JavaScript中预定义函数的基础代码的最佳方法是哪怕是最好的方法.是否有文档说明这些代码是如何编码的,或者是一种简单的方法来查看基础代码?

What is the best way, if even possible, to see the underlying code for the predefined functions in Javascript. Is there documentation that shows how these were coded, or an easy way to actually view the underlying code?

  • parseInt
  • parseFloat
  • isNaN

推荐答案

进一步研究后,我在ECMAScript规范中发现了这一点. http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf

After looking further I found this in the ECMAScript specification. http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf

调用parseInt函数时,将执行以下步骤:

  1. 让inputString为ToString(string).
  2. 让S为inputString的新创建子字符串,该子字符串由第一个字符组成,该字符不是StrWhiteSpaceChar和该字符后的所有字符.(换句话说,删除前导白色空格.)如果inputString不包含任何此类字符,则使S为空字符串.
  3. 让符号为1.
  4. 如果S不为空并且S的第一个字符是减号-,则使符号为1.
  5. 如果S不为空并且S的第一个字符是加号+或减号-,则删除第一个字符来自S.
  6. 让R = ToInt32(基数).
  7. 让stripPrefix为true.
  8. 如果R0,则©Ecma International 2011 105一种.如果R <2或R> 36,然后返回NaN.b.如果R16,则将stripPrefix设置为false.
  9. 否则,R = 0一种.设R = 10.
  10. 如果stripPrefix为true,则一种.如果S的长度至少为2,并且S的前两个字符为"0x"或"0X",则删除S的前两个字符,令R = 16.
  11. 如果S包含不是基数R的任何字符,则令Z为S的子字符串,包括所有在第一个这样的字符之前的字符;否则,让Z为S.
  12. 如果Z为空,则返回NaN.
  13. 让mathInt为以整数R表示的Z表示的数学整数值,使用字母A-Z和a-z用于数值从10到35的数字.(但是,如果R为10并且Z包含超过20有效数字,在20号之后的每个有效数字都可以由0数字代替,具体取决于执行;如果R不是2、4、8、10、16或32,则mathInt可能取决于实现近似为Z以基数R表示的数学整数值.)
  1. Let inputString be ToString(string).
  2. Let S be a newly created substring of inputString consisting of the first character that is not a StrWhiteSpaceChar and all characters following that character. (In other words, remove leading white space.) If inputString does not contain any such characters, let S be the empty string.
  3. Let sign be 1.
  4. If S is not empty and the first character of S is a minus sign -, let sign be 1.
  5. If S is not empty and the first character of S is a plus sign + or a minus sign -, then remove the first character from S.
  6. Let R = ToInt32(radix).
  7. Let stripPrefix be true.
  8. If R  0, then© Ecma International 2011 105 a. If R < 2 or R > 36, then return NaN. b. If R  16, let stripPrefix be false.
  9. Else, R = 0 a. Let R = 10.
  10. If stripPrefix is true, then a. If the length of S is at least 2 and the first two characters of S are either ―0x‖ or ―0X‖, then remove the first two characters from S and let R = 16.
  11. If S contains any character that is not a radix-R digit, then let Z be the substring of S consisting of all characters before the first such character; otherwise, let Z be S.
  12. If Z is empty, return NaN.
  13. Let mathInt be the mathematical integer value that is represented by Z in radix-R notation, using the letters A-Z and a-z for digits with values 10 through 35. (However, if R is 10 and Z contains more than 20 significant digits, every significant digit after the 20th may be replaced by a 0 digit, at the option of the implementation; and if R is not 2, 4, 8, 10, 16, or 32, then mathInt may be an implementation-dependent approximation to the mathematical integer value that is represented by Z in radix-R notation.)
  1. 让number为mathInt的Number值.
  2. 返回符号编号.注意parseInt只能将字符串的开头部分解释为整数值;它会忽略任何字符不能解释为整数符号的一部分,也没有迹象表明任何此类字符都是忽略了.

调用parseFloat函数时,将执行以下步骤:

  1. 让inputString为ToString(string).
  2. 让trimmedString是inputString的子字符串,该子字符串由最左字符而不是StrWhiteSpaceChar和该字符右边的所有字符.(换句话说,删除前导白色空格.)如果inputString不包含任何此类字符,则将trimmedString为空字符串.
  3. 如果trimmedString或trimmedString的任何前缀均不满足StrDecimalLiteral的语法(请参见9.3.1),返回NaN.
  4. 让numberString是trimmedString的最长前缀,它可以是trimmedString本身,它满足StrDecimalLiteral的语法.
  5. 返回numberString的MV的Number值.

注意parseFloat只能将字符串的开头部分解释为Number值;它会忽略任何字符不能解释为十进制文字符号的一部分,也没有迹象表明此类字符是忽略了.

NOTE parseFloat may interpret only a leading portion of string as a Number value; it ignores any characters that cannot be interpreted as part of the notation of an decimal literal, and no indication is given that any such characters were ignored.

如果参数强制为NaN,则返回true,否则返回false.

  1. 如果ToNumber(number)为NaN,则返回true.
  2. 否则,返回false.注意ECMAScript代码测试值X是否为NaN的可靠方法是表达式X!==X.当且仅当X为NaN时,结果才为真.

这篇关于javascript预定义函数(parseInt,parseFloat,isNaN等)的算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆