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


相关推荐

  • JavaScript:三目运算符

    JavaScript:三目运算符HELLO大家好!三目运算符是一个非常简单且使用的运算符。是由两个运算符连接的三个操作数据或者表达式条件表达式?表达式1:表达式0当条件表达式为true则选择表达式1,反之false则选择表达式0举个栗子varage=15;console.log(age<18?’未成年’:’成年’);结果为:···本人写博客就是想记录一下自己所学的知识(目前正在学习中),巩固知识加深记忆,也顺便分享一下自己的所学,有什么地方写的不对,希望大家可以多多指出,让我及时改正。如果我分享的

    2022年6月17日
    33
  • php 取历史值,从新浪获取股票历史数据[通俗易懂]

    php 取历史值,从新浪获取股票历史数据[通俗易懂]转自:http://blog.163.com/fluxray_sensor/blog/static/2965101520085213574929/这几天在网上找股票的历史数据想研究研究,最后找到一个不错的接口,来自于雅虎,是在一个博客上找到的,地址为:http://www.bizeway.net/read.php?317。不过这个不是我第一个找到的接口,最初始我是想直接解析新浪的历史交易页面,不…

    2022年6月24日
    21
  • 多项式除法例子_方程除法怎么算

    多项式除法例子_方程除法怎么算问题描述给定一个$n$次多项式$F(x)$和一个$m$次多项式$G(x)$,请求出多项式$Q(x)$,$R(x)$,满足以下条件:$Q(x)$次数为$n-m$,$R(x)$次数

    2022年8月4日
    4
  • 什么是有氧运动和无氧运动_SKU编码

    什么是有氧运动和无氧运动_SKU编码首先,搞清楚商品与单品的区别。例如,iphone是一个单品,但是在淘宝上当很多商家同时出售这个产品的时候,iphone就是一个商品了。商品:淘宝叫item,京东叫product,商品特指与商家有关的商品,每个商品有一个商家编码,每个商品下面有多个颜色,款式,可以有多个SKU。SPU=StandardProductUnit(标准化产品单元)SPU是商品信息聚合的最小单位,是一组可复用、易检…

    2022年8月30日
    2
  • 控制反转和依赖注入

    控制反转和依赖注入控制反转和依赖注入

    2022年4月23日
    37
  • Spring系列博客汇总

    Spring系列博客汇总https://www.cnblogs.com/leeSmall/category/1093236.htmlspringmvchttps://www.cnblogs.com/leeSmall/ca

    2022年7月3日
    24

发表回复

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

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