Algorithm of predefined javascript functions (parseInt, parseFloat, isNaN, etc.)

What is the best way, if at all possible, to see the base code for predefined functions in Javascript. Is there any documentation that shows how they were encoded, or an easy way to actually view the base code?

  • parseInt
  • parseFloat
  • isNaN
+8
javascript native-code
source share
3 answers

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

When the parseInt function is called, the following steps are performed:

  • Let inputString be ToString (string).
  • Let S be the newly created substring inputString, consisting of the first character, which is not StrWhiteSpaceChar, and all the characters following this character. (In other words, remove the leading white space.) If inputString does not contain such characters, let S be an empty string.
  • Let the sign be 1.
  • If S is not empty and the first character S is a minus sign, then let the sign be 1.
  • If S is not empty and the first character of S is a plus + or minus - sign, then remove the first character from S.
  • Let R = ToInt32 (radix).
  • Let stripPrefix be true.
  • 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.
  • Else, R = 0 a. Let R = 10.
  • If stripPrefix is ​​true, then a. If the length of S is at least 2, and the first two characters of S are -0x‖ or -0X‖, then remove the first two characters from S and R = 16.
  • If S contains any character that is not radix-R, then let Z be the substring of S consisting of all characters before the first such character; otherwise let Z - S.
  • If Z is empty, return NaN.
  • Let mathInt be the mathematical integer that is represented by Z in the radix-R notation using the letters AZ and az for numbers with values ​​from 10 to 35. (However, if R is 10 and Z contains more than 20 significant digits, each significant digit after 20th can be replaced by the number 0, the implementation is optional; and if R is not 2, 4, 8, 10, 16 or 32, then mathInt may be implementation-dependent approximation of the mathematical integer represented by Z in radix-R notation) .
    • Let number be the Number value for mathInt.
    • Return character  number. NOTE parseInt can only interpret the leading part of a string as an integer value; it ignores any characters that cannot be interpreted as part of the notation of an integer, and does not indicate that any such characters have been ignored.

When the parseFloat function is called, the following steps are performed:

  • Let inputString be ToString (string).
  • Let trimmedString be a substring of inputString consisting of the leftmost character, which is not StrWhiteSpaceChar and all characters to the right of this character. (In other words, remove the leading white color of space.) If inputString does not contain such characters, let trimmedString be an empty string.
  • If neither trimmedString nor any trimmedString prefix satisfies the StrDecimalLiteral syntax (see 9.3.1), return NaN.
  • Let numberString be the longest trimmedString prefix, which can be truncated by the string itself, which satisfies the StrDecimalLiteral syntax.
  • Returns the Number value for MV numberString.

NOTE parseFloat can only interpret the leading part of a string as the value of Number; it ignores any characters that cannot be interpreted as part of the decimal notation, and does not indicate that any such characters were ignored.

Returns true if the argument forces NaN and otherwise returns false.

  • If ToNumber (number) is NaN, return true.
  • Otherwise, return false. NOTE. A reliable way for ECMAScript to check if an X NaN value is an expression of the form X! == X. The result will be true if and only if X is NaN.
+2
source share

They are native functions and may be encoded in the language your JS engine is written in, you will need to contact it.

However, you are probably more interested in the EcmaScript specification , which describes how the algorithms work.

And if you're lucky, for some functions you can even find the equivalent of JS. You will find them mainly on pages that test ES implementations against the standard.

+6
source share

These functions are browser-specific and are not written in JS (unless someone decides to write a browser engine in JS). Code is not guaranteed to be the same in different environments, although they should (theoretically) adhere to the ECMAScript specification for their behavior.

0
source share

All Articles