Javascript权威指南

一、数字写法二、Math对象的用法三、数字计算特殊结果NaN就是NotANumberThenot-a-numbervaluehasoneunusualfeatureinJa

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

一、数字写法

3.14  
2345.789  
.333333333333333333  
6.02e23        // 6.02 × 10  
23  
1.4738223E-32  // 1.4738223 × 10  
−32 

二、Math对象的用法

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

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:

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:

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对象

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.关于反斜杠

"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编码)

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位字符,例如:

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • lombok插件安装 idea 搜不到_idea本地安装lombok插件失败

    lombok插件安装 idea 搜不到_idea本地安装lombok插件失败注:此处我是针对2019.3版本,其他版本可能不一样方法一:[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-O3dR50Jw-1594555693053)(https://raw.githubusercontent.com/bakabakabeng/PicGoStore/master/img/20200712194124.png)][外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-K9ZCeMfs-1594555693079)(https://

  • Opencv中width和widthStep的区别

    Opencv中width和widthStep的区别在Opencv的结构Iplimage中,widthStep并不一定等于width*nChannel*(数据类型所占字节),这是因为Opencv中对内存有管理的机制,这一机制会对内存进行对齐,也就是当每一行所占的字节数不等于4的倍数时会自动补齐。例如:width=117,depth=8U,nChannel=1,则widthStep=120,因为117不是4的倍数,所以补齐到120。如果dep

  • 领导力的5个层次书籍_领导力发展的第五个层次是指

    领导力的5个层次书籍_领导力发展的第五个层次是指领导力的5个层次-个人发展—立人者方能立已一、前言第三层次领导力的重点在于个体与团队的生产力。要想获得能够领导精英的较高领导力,领导者必须实现从生产者到培养者的转变。第四层次上的优秀领导者投资时间、精力、金钱和思想,来培养其它领导者。他人观察每一个人的头衔、地位、年龄和阅历,试图度量其发展为领导者的潜力。鉴别并发…

    2022年10月24日
  • Error filterStart的问题

    Error filterStart的问题今天出现这个问题由于只是报了一个error,不能解决问题,所以网上找了找关于这的问题可以在项目的WEB-INF/classes目录下新建一个文件叫logging.properties内容如下

  • 使用BoundsChecker「建议收藏」

     BoundsChecker是一个Run-Time错误检测工具,它主要定位程序在运行时期发生的各种错误。              BoundsChecker能检测的错误包括:3sNews.Net——3S社区&资讯平台tbU^N@i7pwMVBe    1)指针操作和内存、资源泄露错误,比如:内存泄露;资源泄露;对指针变量的错误操作。   

  • 实例分割–Mask RCNN详解(ROI Align / Loss Fun)

    实例分割–Mask RCNN详解(ROI Align / Loss Fun)1总结架构与主要思想未完待续。。。

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号