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

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

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

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 移动端网页实现拨打电话功能的几种方法

    移动端网页实现拨打电话功能的几种方法

    2021年10月17日
    87
  • 连接kafka报错:1 partitions have leader brokers without a matching listener「建议收藏」

    连接kafka报错:1 partitions have leader brokers without a matching listener「建议收藏」服务输出部分错误日志截图2020/12/25下午2:32:442020-12-2514:32:44.320WARN[tag-service,,,]1—[ntainer#4-0-C-1]org.apache.kafka.clients.NetworkClient:[ConsumerclientId=consumer-5,groupId=statistic-processor]1partitionshaveleaderbrokerswithoutamatchi

    2022年6月26日
    170
  • 解决SecureCRT 中文乱码

    解决SecureCRT 中文乱码在linux服务器上搭建solr,用的是SecureCRT 连接linux服务器,发现不能输入中文,配置文件中的中文也是乱码;先以为是SecureCRT工具编码的问题,把编码改成utf-8之后发现还是有乱码;最后才发现其实还有一个地方没有改配置,那就是字体必须是中文字体,而且字符集得是支持中文的字符集如gb2312:还有就是这里不能选择带@的中文字体,不然字体就是躺着的

    2022年7月17日
    15
  • Tomcat国内镜像下载地址【速度超快】

    Tomcat国内镜像下载地址【速度超快】https://mirrors.cnnic.cn/apache/tomcat/ 选择想下载的版本,最新的就是那个tomcat-9 继续点进去 进去以后,要下载的可执行文件就在Bin目录下 exe结尾的就是安装文件了 …

    2022年5月18日
    43
  • 中外数学教学名著与数学思想「建议收藏」

    中外数学教学名著与数学思想(2011-08-0113:30:56)标签:校园分类:工作篇中外数学教学名著 一、数学纵横1.1华罗庚,华罗庚科普著作选集,沪教,84[必读]1.2张奠宙,数学的明天,桂教,99[纵论数学与数学教育,书中的一些观点高屋建瓴,发人深省。系“走向科学的明天丛书”之一,数学方面另有:

    2022年4月8日
    33
  • 如何使用ccs软件创建工程

    这里简叙如何使用ccs软件创建工程的具体步骤,正文如下:1、打开ccs软件,单击菜单栏中的“Project”选项,在弹出的下拉菜单中选择“NewCCSProject”,如下图所示:  2、在“Projectname”中输入自定义的工程名称,在“Family”中选择所需DSP的系列,在“Variant”的目录下选择所需的DSP的型号,最后在“Connection”目录下选择仿真…

    2022年4月8日
    408

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

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