- 形參坝橡,是在定義函數(shù)時使用的參數(shù),目的是用來接收調(diào)用該函數(shù)時傳進(jìn)來的實際參數(shù)肝陪。
- 實參驳庭,:是在調(diào)用時傳遞給函數(shù)的參數(shù)
例如
function myfun(a,b,c){
...
}
myfun(1,2,3);
這里 a刑顺、b氯窍、 c是形參饲常,1,2狼讨,3是實參贝淤,需要注意的是:實參的數(shù)量可以比形參的數(shù)量多的,為被賦值的形參為undefined
ajax數(shù)據(jù)請求
一個完整的ajax的post數(shù)據(jù)請求
var xhr=new XMLHttpRequest();
xhr.open("get",url,true);
xhr.send(null);
xhr.onreadystatechange =function(){
if (xhr.readyState==4&& xhr.status==200) {
console.log(xhr.responseText);
}
}
ajax返回promise對象
簡單的ajax數(shù)據(jù)請求政供,通過promise對象的then播聪、catch 方法
function ajax(method,url,data){
var xhr=new new XMLHttpRequest();
return new Promise(function (resolve,reject){
xhr.onreadystatechange=function(){
if (xhr.readyState==4&&xhr.status==200) {
//如果成功獲取數(shù)據(jù)
resolve(xhr.responseText)
}else {
//如果失敗,則返回狀態(tài)碼
reject(xhr.status);
}
};
xhr.open(method,url);
xhr.send(data);
})
}
// ----
var p = ajax("get",url);
p.then(function(res){
console.log(res);
}).catch(function(err){
console.log(err)
})
promise對象是一個異步編程
- 回調(diào)函數(shù)
通過向函數(shù)中添加回調(diào)函數(shù)布隔,實現(xiàn)異步操作 - promise
let p = new Promise();
構(gòu)造函數(shù)的參數(shù)是一個函數(shù)离陶,這個函數(shù)又有兩個函數(shù)(resolve,reject)衅檀,這兩個函數(shù)也是函數(shù)招刨,第一個參數(shù)resolve,可以看做是請求成功哀军,resolve函數(shù)的作用是將 pending(正在進(jìn)行的狀態(tài)) ---》resolved(成功)沉眶,同時將成功的結(jié)果通過參數(shù)又傳遞出去;reject函數(shù)的作用 pending---》rejecteds(失斏际省);