前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Javascript权威指南

Javascript权威指南

作者头像
全栈程序员站长
发布2021-12-27 08:22:07
8220
发布2021-12-27 08:22:07
举报
文章被收录于专栏:全栈程序员必看

一、数字写法

代码语言:javascript
复制
3.14  
2345.789  
.333333333333333333  
6.02e23        // 6.02 × 10  
23  
1.4738223E-32  // 1.4738223 × 10  
−32 

二、Math对象的用法

代码语言:javascript
复制
Math.pow(2,53)           // => 9007199254740992: 2 to the power 53  
Math.round(.6)           // => 1.0: round to the nearest integer  
Math.ceil(.6)            // => 1.0: round up to an integer  
Math.floor(.6)           // => 0.0: round down to an integer  
Math.abs(-5)             // => 5: absolute value  
Math.max(x,y,z)          // Return the largest argument  
Math.min(x,y,z)          // Return the smallest argument  
Math.random()            // Pseudo-random number x where 0 <= x < 1.0  
Math.PI                  // π: circumference of a circle / diameter  
Math.E                   // e: The base of the natural logarithm  
Math.sqrt(3)             // The square root of 3  
Math.pow(3, 1/3)         // The cube root of 3  
Math.sin(0)              // Trigonometry: also Math.cos, Math.atan, etc.  
Math.log(10)             // Natural logarithm of 10  
Math.log(100)/Math.LN10  // Base 10 logarithm of 100  
Math.log(512)/Math.LN2   // Base 2 logarithm of 512  
Math.exp(3)              // Math.E cubed  /* 何问起 hovertree.com */

三、数字计算特殊结果

NaN就是Not A Number

代码语言:javascript
复制
Infinity                    // A read/write variable initialized to Infinity.  
Number.POSITIVE_INFINITY    // Same value, read-only.  
1/0                         // This is also the same value.  
Number.MAX_VALUE + 1        // This also evaluates to Infinity.  
Number.NEGATIVE_INFINITY    // These expressions are negative infinity.  
-Infinity  
-1/0                          
-Number.MAX_VALUE - 1  
NaN                         // A read/write variable initialized to NaN.  
Number.NaN                  // A read-only property holding the same value.  
0/0                         // Evaluates to NaN.  
Number.MIN_VALUE/2          // Underflow: evaluates to 0  
-Number.MIN_VALUE/2         // Negative zero  
-1/Infinity                 // Also negative 0  
-0  
/* 何问起 hovertree.com */

The not-a-number value has one unusual feature in JavaScript: it does not compare equal to any other value, including itself.

 This means that you can’t write x == NaN to determine whether the value of a variable x is NaN. Instead, you should write x != x.

That expression will be true if, and only if, x is NaN. The function isNaN() is similar. 

It returns true if its argument is NaN, or if that argument is a non-numeric value such as a string or an object. 

The related function isFinite() returns true if its argument is a number other than NaN, Infinity, or -Infinity. 

The negative zero value is also somewhat unusual. It compares equal (even using Java-Script’s strict equality test) to positive zero, which means that the two values are almost indistinguishable, except when used as a divisor:

代码语言:javascript
复制
var zero = 0;         // Regular zero  
var negz = -0;        // Negative zero  
zero === negz         // => true: zero and negative zero are equal   
1/zero === 1/negz     // => false: infinity and -infinity are not equal  
// 何问起 hovertree.com

四、十进制小数产生的误差

这货对二进制数的精确度支持的很好,十进制就不行

JavaScript numbers have plenty of precision and can approximate 0.1 very closely. But the fact that this number cannot be represented exactly can lead to problems. Consider this code:

代码语言:javascript
复制
var x = .3 - .2;    // thirty cents minus 20 cents  
var y = .2 - .1;    // twenty cents minus 10 cents  
x == y              // => false: the two values are not the same!  
x == .1             // => false: .3-.2 is not equal to .1  
y == .1             // => true: .2-.1 is equal to .1 
// 何问起 hovertree.com

好在这个问题只会在对值进行比较的时候才会发生。

五、Date对象

代码语言:javascript
复制
var then = new Date(2010, 0, 1);  // The 1st day of the 1st month of 2010  
var later = new Date(2010, 0, 1,  // Same day, at 5:10:30pm, local time  
                     17, 10, 30);  
var now = new Date();          // The current date and time  
var elapsed = now - then;      // Date subtraction: interval in milliseconds   
later.getFullYear()            // => 2010  
later.getMonth()               // => 0: zero-based months  
later.getDate()                // => 1: one-based days  
later.getDay()                 // => 5: day of week.  0 is Sunday 5 is Friday.  
later.getHours()               // => 17: 5pm, local time  
later.getUTCHours()            // hours in UTC time; depends on timezonelater.toString()   
later.toString()               // => "Fri Jan 01 2010 17:10:30 GMT-0800 (PST)"  
later.toUTCString()            // => "Sat, 02 Jan 2010 01:10:30 GMT"  
later.toLocaleDateString()     // => "01/01/2010"  
later.toLocaleTimeString()     // => "05:10:30 PM"  
later.toISOString()            // => "2010-01-02T01:10:30.000Z"; ES5 only  
// 何问起 hovertree.com

六、String对象

1.关于反斜杠

代码语言:javascript
复制
"two\nlines"   // A string representing 2 lines written on one line  
"one\          // A one-line string written on 3 lines. ECMAScript 5 only.  
 long\  
 line"  
// 何问起 hovertree.com

反斜杠可作为js中的转义字符出现,例如:’You\’re right, it can\’t be a quote’。反斜杠后面加上特定的字母会有特殊的含义,比如\n就是换行符,下面举例说明:

\0 The NUL character (\u0000) \b Backspace (\u0008) \t Horizontal tab (\u0009) \n Newline (\u000A) \v Vertical tab (\u000B) \f Form feed (\u000C) \r Carriage return (\u000D) \” Double quote (\u0022) \’ Apostrophe or single quote (\u0027) \\ Backslash (\u005C) \x XX The Latin-1 character specified by the two hexadecimal digits XX \u XXXX The Unicode character specified by the four hexadecimal digits XXXX

除以上形式以外,其他的字符前面加反斜杠之后,反斜杠将被忽略,例如:\#就如同#一样

2.String对象的一些常用方法(使用utf-16编码)

代码语言:javascript
复制
var s = "hello, world"        // Start with some text.  
s.charAt(0)                   // => "h": the first character.  
s.charAt(s.length-1)          // => "d": the last character.  
s.substring(1,4)              // => "ell": the 2nd, 3rd and 4th characters.  
s.slice(1,4)                  // => "ell": same thing  
s.slice(-3)                   // => "rld": last 3 characters  
s.indexOf("l")                // => 2: position of first letter l.  
s.lastIndexOf("l")            // => 10: position of last letter l.  
s.indexOf("l", 3)             // => 3: position of first "l" at or after 3  
s.split(", ")                 // => ["hello", "world"] split into substrings  
s.replace("h", "H")           // => "Hello, world": replaces all instances  
s.toUpperCase()               // => "HELLO, WORLD"  
// 何问起 hovertree.com

String类型的数据可被当成是只读的数组,我们可以访问其中独立的16位字符,例如:

代码语言:javascript
复制
s = "hello, world";  
s[0]                  // => "h"  
s[s.length-1]         // => "d"  
// 何问起 hovertree.com

《Javascript权威指南》学习笔记之~Chapter 3. Type, Values, and Variables

推荐:http://www.cnblogs.com/roucheng/p/texiao.html

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/120412.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档