您的位置:首页 > Web前端

Professional JS(3.4.5The Number Type&3.4.6The String Type)

2017-07-02 09:37 357 查看
1.The Number Type

①Perhaps the most interesting data type in ECMAScript is Number,which uses the IEEE-754 format to represent both integers and floating-point values(also called double-precision(精度) values in some language).To support the various types of numbers,there are several
different number literal formats.

②The most basic number literal format is that of a decimal integer(十进制数),which can be entered directly.

③Integer can also be represented as either octal(八进制) or hexadecimal literals.

④For an octal literal,the first digit(数字) must be a zero(0) followed by a sequence of octal digits(numbers 0 through 7).If a number out of this range is detected(检测) in the literal,then the leading zero is ignored and the number is treated as a decimal,as in
the following examples:

var ocatlNum1=070;//octal for 56
var octalNum2=080;//invalid(无效的) octal ---interpreted as 8
var octalNum3=079;//invalid octal---interpreted as 79
*⑤Octal literals are invalid when running in strict mode and will cause the JS engine to throw a syntax error.

⑥To create a hexadecimal literal,you must make the first two characters 0x(case insensitive,x大小写行),followed by any number of hexadecimal digits(0 through 9,and A through F).Letters may be in uppercase or lowercase.

*⑦Numbers created using octal or hexadecimal format are treated as decimal numbers in all arithmetic 

operations(算术运算).

*⑧Because of the way that numbers are stored in JS,it is actually possible to have a value of positive zero(正零) and negative zero(负零).Positive zero and negative zero are considered equivalent in all cases but are noted in this text for clarity(清楚).(只是个说明)

1.1

①Storing floating-point values uses twice as much memory as storing integer values,ECMAScript always looks for ways to convert values into integers.
var a=1.; //missing digit after point ---interpreted as integer 1
var b=10.0;//whole number ---interpreted as integer 10
②The format of e-notation in ECMAScript is to have a number(integer or floating-point) followed by an uppercase or lowercase letter E,followed by the power of 10 to multiply by.

③You should never test for specific floating-point values,such as if(a+b==0.3){...}

④It's important to understand that rounding errors(舍入误差) are a side effect of the way floating-point arithmetic is done in IEEE-754-based numbers and is not unique(独一无的) to ECMAScript.Other languages that use the same format have the same issues.

⑤Number.MIN_VALUE:5e-324/Number.MAX_VALUE:1.7976931348623157e+308

1.2NaN:

①NaN,which is used to indicate when an operation(操作数) intended to return a number has failed.(as opposed to throwing an error).

②NaN is a special numeric value.

③The value NaN has a couple of unique properties.First,any operation(操作) involving NaN always returns NaN.which can be problematic in the case of multiply step computations(多步计算).Second,NaN is not equal to any value,including NaN.

④Although typically not done(有点不可思议),isNaN() can be applied to(适用于) objects.In that case,the object's valueof() method is first called to determine if the returned value can be converted into a number.If not,the toString() method is called and its returned
value is tested as well.This is general way that built-in functions(内置函数) and operators(操作符) work in ECMAScript.

1.3Number Conversions:

a)Number(),适用于任何数据类型
Number(null);//0
Number(undefined);//NaN
Number("011");//11
Number("");//0
b)parseInt(),只适用于字符串(string)

*①If this first character isn't a number,the minus sign,or the plus sign,parseInt() always returns NaN,which means the empty string returns NaN(unlike with Number(),which returns 0);
Number("");//0
parseInt("");//NaN
②parseInt() provides a second argument:the radix(基数)(number of digits to use).
parseInt("010",2);//2
parseInt("010",8);//8
parseInt("010",10);//10
parseInt("010",16);//16
c)parseFloat(),只适用于字符串(string)

①A decimal point is valid the first time it appears.but a second point is invalid and the rest of the string is ignored,resulting in "22.34.5" being converted to 22.34

②distinction:Number()&parseInt()&parseFloat()

1.Number()忽略前导的零,但不忽略十六进制0x中的零。/the same as parseInt()

2.parseFloat()始终忽略前导的零,因此十六进制的字符串总是返回0

③A final note:if the string represents a whole number(no decimal point or only a zero after the decimal 

point),parseFloat() returns an integer.
parseFloat("1234.00000orange");//1234
2.The String Type:

①The string data type represents a sequence of zero or more 16-bit Unicode characters.

②\r:carriage return.

③\f:form feed.

④\xnn:A character represents by hexadecimal code nn(where n is a hexadecimal digit 0-F).

⑤\unnnn:A Unicode character represents by the hexadecimal code nnnn(where n is a hexadecimal digit 0-F).

⑥字符串特点(不可变):
var a="Java";
a=a+"Script";
This happens by creating a new string with enough space for 10 characters and then filling that string with "Java" and "Script".The last step in the process is destroy the original string "Java" and the string "Script",because neither is necessary
anymore.

⑦Converting to a String:除了null,undefined,其他数据类型都可以用toString()方法(Number,String,Boolean,Object)

var a=10;
a.toString();//"10"
a.toString(2);//"1010"
a.toString(8);//"12"
a.toString(10);//"10"
a.toString(16);"a"
⑧If you're not sure that a value isn't null or undefined,you can use the String() casting function(转型函数),which always returns a string regardless of the value type.
var a=null;
var b;
String(a);//"null"
String(b);//"undefined"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐