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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Struts2–自定义拦截器三种方式(实现Interceptor接口、继承抽象类AbstractInterceptor、继承MethodFilterInterceptor)「建议收藏」

    Struts2–自定义拦截器三种方式(实现Interceptor接口、继承抽象类AbstractInterceptor、继承MethodFilterInterceptor)「建议收藏」实现自定义拦截器在实际的项目开发中,虽然Struts2的内建拦截器可以完成大部分的拦截任务,但是,一些与系统逻辑相关的通用功能(如权限的控制和用户登录控制等),则需要通过自定义拦截器实现。本节将详细讲解如何自定义拦截器。1.实现Interceptor接口在Struts2框架中,通常开发人员所编写的自定义拦截器类都会直接或间接地实现com.opensymphony.xwork2.in…

    2022年5月14日
    43
  • python中的静态方法和类方法

    python中的静态方法和类方法

    2021年11月19日
    49
  • 如何在firefox 7的地址栏里显示http://前缀

    如何在firefox 7的地址栏里显示http://前缀

    2021年8月18日
    56
  • Java学习之动态代理篇

    Java学习之动态代理篇0x00前言在后面的漏洞研究的学习中,必须要会的几个知识点。反射机制和动态代理机制。至于反射的前面已经讲到过了,这里就不做更多的赘述了。0x01动态代理这里先来讲一

    2021年12月12日
    55
  • POE交换机通用吗_工业级交换机

    POE交换机通用吗_工业级交换机目前PoE交换机需要量大,那一定有些人疑惑PoE交换机能够替代一般工业交换机应用吗?下面为大伙儿介绍下,一起来瞧瞧吧。一般状况下是还可以的,具备IEEE802.3af或是IEEE802.3at协议书的POE交换机,输出电压时会有一个小电流量侦测。假如另一方不是带PoE的设备,那么就不容易供以往48V的工作电压。可是PoE交换机虽具有交换机的作用,作为一般工业交换机应用时,沒有最大限度充分发挥它的使用价值,不足经济发展节省,是自然资源的消耗。假如不用对联接设备给予直流电,能够同时采用一般工业.

    2022年10月5日
    4
  • 还原对于服务器失败 备份集中的数据库备份与现有数据库不同

    还原对于服务器失败 备份集中的数据库备份与现有数据库不同还原对于服务器失败备份集中的数据库备份与现有数据库不同    今天在SQLServer2008R2中还原一个数据库备份,遇到错误。还原对于服务器失败备份集中的数据库备份与现有数据库不同。    解决方案有以下几种,一般能够成功:在恢复新建数据库时,没有选中“覆盖原数据库”。    解决方法:选中用于还原的备份集,在选项中,勾选“覆盖现有数据库”

    2022年6月5日
    45

发表回复

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

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