失效链接处理 |
How JavaScript Works PDF 下载
相关截图:
主要内容:
Number (not number, the initial uppercase N is significant) is a function that can make
numbers. Numbers in JavaScript are immutable objects. The typeof operator returns
"number" (the initial lowercase n is significant) when given a number. You should
never use the new prefix with the Number function. It does not do what you might think
it should do.
const goocLexample = Number("432");
const bacLexample = new Number("432");
typeof goocLexample
// "number"
typeof bacLexample
// "object"
goocLexample === bacLexample
//false
Number is also a container of some constants. These constants can provide insight into
the workings of numbers.
Number.EPSILON is exactly 2.220446049250313080847263336181640625e-16. It is the
smallest positive number that, when added to 1, produces a sum that is larger than
1. Adding any positive number that is less than Number.EPSILON to 1 produces a sum
that is exactly 1. It should seem absurd that adding a number that is not 0 to 1 yields
1. This is not due to a bug or design error in JavaScript. All fixed size floating point
systems have such weirdness, including IEEE 754. It is a reasonable tradeoff.
Number.MAX_SAFE_INTEGER is exactly 9007199254740991, or about 9 quadrillion in the
modern illion idiom. JavaScript does not have or need an integer type because its
number type can exactly represent all of the integers up to Number.MAX_SAFE_INTEGER.
JavaScript has 54 bit signed integers within its number type.2.3
How Numbers Work
Adding 1 to an integer that is larger than Number. MAX_SAFE_INTEGER has the same effect
as adding 0 to that large number. JavaScript is capable of exact integer arithmetic
so long as all of the values and results and intermediate results are integers that lie
between -Number.MAX_SAFE_INTEGER and Number.MAX_SAFE_INTEGER. Within that range,
conventional mathematical reasoning can hold. The Associative Law and Distributed
Law are in effect. Outside of that range, things are more chaotic. For example, the
order in which you add a sequence of numbers can change the sum. So, ((0.1
+ 0.2) + 0.3) produces a larger result than (0.1 + (0.2 + 0.3)). Number
. isSafe!nteger(num&er) returns true if the number is in the safe range.
Number. islnteger(num&er) returns true if the number is an integer in the safe range
or ifit is above the safe range. All numbers greater than Number. MAX_SAFE_INTEGER are
considered to be integers. Some of them are exactly correct. Most of them are not.
Number.MAX_VALUE contains the largest number that JavaScript can represent. It is
exactly Number.MAX_SAFE_INTEGER * 2 ** 971 or
17976931348623157081452742373170435679807056752584499659891747680315726078002853
87605895586327668781715404589535143824642343213268894641827684675467035375169860
49910576551282076245490090389328944075868508455133942304583236903222948165808559
332123348274797826204144723168738177180919299881250404026184124858368
which is 1 followed by 308 digits. Most of that is phantom significance. These numbers
can provide 15.9 digits of significance. The trailing 292 digits are an illusion caused
by base 2 beating against base 10.
Adding any positive safe integer to Number.MAX_VALUE produces a sum that is also
Number.MAX_VALUE. It is likely that if a program produces a result that is Number
.MAX_VALUE then the program is in error. Any result that exceeds Number
.MAX_SAFE_INTEGER is suspect. The IEEE 754 standard promises the potential of
enormous range, but without extraordinary care, it is more likely to lead to mistakes.
Number.MIN.VALUE is the smallest number that can be represented that is larger than
zero. It is exactly equal to 2 ** -1074 or
4.940656458412465441765687928682213723650598026143247644255856825006755072702087
51865299836361635992379796564695445717730926656710355939796398774796010781878126
30071319031140452784581716784898210368871863605699873072305000638740915356498438
73124733972731696151400317153853980741262385655911710266585566867681870395603106
24931945271591492455329305456544401127480129709999541931989409080416563324524757
14786901472678015935523861155013480352649347201937902681071074917033322268447533
35720832431936092382893458368060106011506169809753078342277318329247904982524730
77637592724787465608477820373446969953364701797267771758512566055119913150489110
14510378627381672509558373897335989936648099411642057026370902792427675445652290
87538682506419718265533447265625e-324
All positive numbers that are smaller than Number.MIN.VALUE are indistinguishable
from zero. Note that the significand of Number.MIN.VALUE contains only a single bit in
the least significant position. That lonely bit produces a lot of phantom significance.
Number.prototype is an object that all numbers inherit from. Number.prototype
contains a set of methods. Unfortunately, it is not a very useful set of methods.
|