1.js數字-NaN和Infinity
1、在js中,有一種特殊的數值,叫做NaN(Not a Number),表示本來要返回數值的操作卻未返回數值的情況,例如0除以0的操作,在其它語言中會報錯誤或異常,而在js中,0/0會返回NaN,不中斷代碼執行。
0/0; //NaN
2、任何涉及NaN的運算都會返回NaN,例如
1 + 0/0; //NaN
3、NaN與任何值都不相等,包括自身,例如
console.log(NaN == NaN); //false
js提供了isNaN方法用來判斷是否是NaN,如
isNaN(0/0); //true
4、Infinity用于表示正無窮大,- Infinity用于表示負無窮大,例如:
1/0; //Infinity
-1/0; //-Infinity
5、Infinity與Nan
Infinity * 0; //NaN
1、0/0返回NaN
2、1/0返回Infinity
3、Infinity*0返回NaN
4、NaN參與運算都返回NaN
5、NaN不等于NaN
6、用isNaN判斷是否為NaN
二.js類型-字符串
1、在js中,字符串可以用單引號(’)或雙引號(’’)表示,例如
var str1 = 'hello', str2 = "hello";
-
單引號中帶雙引號
-
雙引號中帶雙引號
-
雙引號中帶雙引號
var str = 'hello, "張三"'; console.log(str);str = "hello, '張三'"; console.log(str);str = "Hello, \"張三\""; console.log(str);
?
1、可以在程序中直接使用的數據值,叫直接量。
如:
12 //表示是數字 1.2 //表示是小數 "hello world" //表示字符串 'Hello' //表示字符串 true/flase //表示布爾值 null //空值 {a:1,b:2} //表示對象 [a,b,c,d,e] //表示數組
五、字符串使用
1、使用“+”運算符可以將字符串拼接在一起。
var a = "Hello" +"world!";
2、String是一個內置的對象,此對象提供了許多可以調用的常用方法。
var s = "hello javascript";
s.charAt(0);//返回第一個字符
s.charAt(s.length -1); //返回最后一個字符
s.substring(1,4);//取子串
s.indexOf("o");//返回該字符出現的位置
s.lastIndexOf("a");//返回最后一次字符出現位置
s.split(" ");//返回按空格分割成的數組字符
s.replace("h","H");//字符替換
s.toUpperCase();//字符串轉成大寫
注意:字符串是固定不變的,字符串方法都返回新的字符串。
六、js的全局對象
1、當js解釋器啟動時(或瀏覽器加載新頁面時),將創建一個新的全局對象,并給它一些初始屬性:
全局屬性:
- undefined :未定義值
- Infinity :正的無窮大
- NaN : 值是不是數字值
全局函數:’
- isNaN() : 判斷是否是非數字值。
- eval() : 將字符串當作腳本代碼來執行
- parseFloat() :解析字符串并返回浮點數
- parseInt() :解析字符串并返回整數
- decodeURI(): 解碼URI
- encodeURI() : 將字符串編碼
- String():轉換為字符串
- Number():轉換為數字
- Date():轉換為日期
2、可以用this關鍵字引用全局對象。
var g = this;
console.log(isNaN(123)); console.log(parseInt("123")+5); console.log(parseFloat("123.11")); var url = encodeURI("http://www.itbegin.com/it學習/"); console.log(url);//編碼后空格、中文會轉碼 console.log(decodeURI(url)); console.log(String("abc")); console.log(Number("123.11")+456); var s = parseInt("123.11")+456; console.log(s); var global = this;//定義了引用全局對象的全局變量 console.log(global.parseInt("123"));
?
八、顯式類型轉換
1、js里很多是自動做類型轉換。
var a = 1;//a的類型的數字
a = "abc";//這里a的類型變為字符型
2、顯式轉換,可以讓代碼可讀性更強。
var a = Number("3");
var s = String(false);
var b = Boolean([]);
3、js里某些運算符會做隱式的類型轉換。
var a = 1; a+"";//此表達式就會轉換為字符串
console.log(Number("123"));//字符串轉數字 console.log(String(false));//布爾值轉字符串 var b = Boolean("false"); console.log(b);//字符串轉布爾值var n = 10; console.log(n.toString());//數字轉字符串 console.log(n.toString(2));//數字轉二進制字符串 console.log(parseInt("112 abc112"));//盡可能的解析數字字符,并忽略后面的內容。 console.log(parseFloat("3.55 abc")); console.log(parseFloat(".1")); console.log(parseInt("0.1")); console.log(parseInt(".1")); console.log(parseFloat("$88.12"));
一、核心JavaScript內置對象,即ECMAScript實現提供的不依賴于宿主環境的對象
這些對象在程序執行之前就已經(實例化)存在了。ECMAScript稱為The Global Object,分為以下幾種
1, 值屬性的全局對象(Value Properties of the Global Object)。有NaN,Infinity,undefined。
2, 函數屬性的全局對象(Function Properties of the Global Object)。有eval,parseInt,parseFloat,isNaN,isFinite,decodeURI,encodedURI,encodeURIComponent
3,構造器(類)屬性的全局對象(Constructor Properties of the Global Object)。有Object,Function,Array,String,Boolean,Number,Date,RegExp,Error,EvalError,RangeError,ReferenceError,SyntaxError,TypeError,URIError。
4,其它屬性的全局對象(Other Properties of the Global Object),可以看出成是Java中的靜態類,可以直接用類名+點號+方法名使用。有Math,JSON。
ECMAScript規范提到這些全局對象(The Global Object)是具有Writable屬性的,即Writable為true,枚舉性(Enumerable)為false,即不能用for in枚舉。ECMAScript有這么一段
Unless otherwise specified, the standard built-in properties of the global object have attributes {[[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: true}.
1、最簡單的表達式是“原始表達式”,它包括常量或直接量、關鍵字和變量。
var b = 1.1;
var s = "itbegin";
2、對象初始化表達式實際是用來新創建的對象。
var obj = {a:1,b:2};
3、數組初始化表達式實際是用來新創建一個數組。
var a = [];
var b = [1,2,3];
var c = [1,,4];//數組4個元素,其中兩個是undefined
4、函數定義表達式,用來定義一個新的函數。
var area = function(r){return 3.14*r*r;}//圓面積函數
5、屬性表達式,通過“.”符號來訪問對象的屬性。
var o = {a:1,b:1};
o.a;//表達式o的a屬性
o.b;//表達式o的b屬性
6、調用表達式是一種調用函數或方法的語法。
f();//調用函數f(