JavaScript的系統函數學習 (轉)
- 編碼處理函數
???????? 1) encodeURI
???????????????? 返回一個對URI字符串編碼后的結果。URL是最常見的一種URI;
?????????2) decodeURI
?????????????????將一個已編碼的URI字符串解碼成最原始的字符串返回;
?????????3)? 舉例:








???? encodeStr:? http://www.amigoxie.com/index.jsp?name=%E9%98%BF%E8%9C%9C%E6%9E%9C
???? decodeStr:? http://www.amigoxie.com/index.jsp?name=阿蜜果
?? 2. 數值處理函數
????? 1)?? parseInt
??????????? ? 將一個字符串指定的進制轉換為一個整數,語法格式為:
?????????????? parseInt(numString, [radix])
?????????????? 第一個參數是要進行轉換的字符串,是介于2到36之間的數值,用于指定進行字符串轉換時所用的進制。
??????????????? 舉例如下:


































??????默認情況下的結果:32:32;032:26;0x32:50
??????轉為2進制的結果:32:NaN;032:0;0x32:0
??????轉為8進制的結果:32:26;032:26;0x32:0
??????轉為16進制的結果:32:50;032:50;0x32:50
??????11001010轉換后的結果:?
??????2進制:202;16進制:285216784
??????8進制:2359816;10進制:11001010
??????43abc轉換后:43;abc43轉換后:NaN;abc轉換后:NaN?
?????2) parseFloat方法
??????????? 該方法將一個字符串轉換成對應的小數。
??????????? eg.







???????? 4.11
???????? 5.1
??????? 3)?isNaN方法
???????????????該方法用于檢測前兩個方法返回值是否為非數值型,如果是,返回true,否則,反回false。
?????????????? eg.?
??????????????















????? parseInt('amigo1121')的結果是:?NaN
?????
??????3 字符串編碼處理函數
????????? 1)? escape方法
?????????????? 該方法返回對一個字符串編碼后的結果字符串,所有空格, 標點, 重音符號以及任何其他非ASCII碼字符都用%xx編碼替換,其中xx等于表示該字符的Unicode編碼的十六進制數.空格返回為"%20", 字符值大于255的字符以%xxxx格式存儲.
????????????? eg.






???????? 2)? unescape方法
??????????????該方法將一個用escape方法編碼的結果字符串解碼成原始字符串.
????????????? eg.







???????? 3)? eval方法
????????????? 該方法將某個參數字符串作為一個JavaScript執行.
//定義JavaScript對象的幾種格式
// Method 1: flat array style quick object define
var myObject = {
username : "beansoft",
age : 24,
test : function() {alert(this.age);}
};
// Method 2: using Object
var myObject = new Object();
myObject.username = "beansoft";
myObject.age = 24;
// Method 3: using constructor
function MyObject(username, age) {
this.username = username;
this.age = age;
this.test = function() {alert(this.age);};
}
var myObject = new MyObject("beansoft", 24);
// Using: myObject.username, myObject["username"], myObject[0]
myObject.test();// Will display alert window, value is age
myObject.username = "Hello";// Will asign the username property to "Hello"??
??
// Method 1: flat array style quick object define
var myObject = {
username : "beansoft",
age : 24,
test : function() {alert(this.age);}
};
// Method 2: using Object
var myObject = new Object();
myObject.username = "beansoft";
myObject.age = 24;
// Method 3: using constructor
function MyObject(username, age) {
this.username = username;
this.age = age;
this.test = function() {alert(this.age);};
}
var myObject = new MyObject("beansoft", 24);
// Using: myObject.username, myObject["username"], myObject[0]
myObject.test();// Will display alert window, value is age
myObject.username = "Hello";// Will asign the username property to "Hello"??
??
語法
with (object)
statement
with 語句的語法組成如下:
部分 描述
object 新的默認對象。
statement 一個語句,object 是該語句的默認對象。可以是復合語句。
說明
with 語句通常用來縮短特定情形下必須寫的代碼量。在下面的例子中,請注意 Math 的重復使用:
x = Math.cos(3 * Math.PI) + Math.sin(Math.LN10)
y = Math.tan(14 * Math.E)
當使用 with 語句時,代碼變得更短且更易讀:
with (Math)
{
x = cos(3 * PI) + sin (LN10)
y = tan(14 * E)
}
當我們操作一個很長的對象的時候, 可以減少一點代碼量, 例如:
with(someobj.style) {
dispaly = 'none';
}?
with (object)
statement
with 語句的語法組成如下:
部分 描述
object 新的默認對象。
statement 一個語句,object 是該語句的默認對象。可以是復合語句。
說明
with 語句通常用來縮短特定情形下必須寫的代碼量。在下面的例子中,請注意 Math 的重復使用:
x = Math.cos(3 * Math.PI) + Math.sin(Math.LN10)
y = Math.tan(14 * Math.E)
當使用 with 語句時,代碼變得更短且更易讀:
with (Math)
{
x = cos(3 * PI) + sin (LN10)
y = tan(14 * E)
}
當我們操作一個很長的對象的時候, 可以減少一點代碼量, 例如:
with(someobj.style) {
dispaly = 'none';
}?
posted on 2007-03-12 13:58 liaojiyong 閱讀(378) 評論(0) 編輯 收藏 所屬分類: Ajax