web前端筆試題/面試題小結(2)

//1、原生ajax
var xmlRequest = createXmlRequest();ajax

//每當 readyState 改變時,就會觸發 onreadystatechange 事件。
xmlRequest.onreadystatechange = function() {服務器

/**
* readyState
* 0(未初始化)
* 1(初始化)
* 2(發送數據)
* 3(數據傳送中)
* 4(完成)
* status
* 200 請求成功,有數據返回
* 204 請求成功,無數據返回
*
*
**/
if (xmlRequest.readyState == 4 && (xmlRequest.status == 200 ||  xmlRequest.status == 204)) {
console.log(xmlRequest.responseText)
}
}
*
* open(method,url,async)
*
* method:請求的類型;GET 或 POST
* url:文件在服務器上的位置
* async:true(異步)或 false(同步)
*
*
* send(string) 將請求發送到服務器
* string:僅用於POST請求
*
*
xmlRequest.open("POST", 'url', false);
xmlRequest.send(null);閉包


function createXmlRequest() {
var xmlRequest = null;
try {
xmlRequest = new XMLHttpRequest();app

} catch (e) {
try {
xmlRequest = new ActiveXObject("Msxml2.XMLHTTP"); //IE6;
} catch (e) {
xmlRequest = new ActiveXObject("Microsoft.XMLHTTP"); //IE5
}
}
return xmlRequest;
}異步

//2、繼承
var Fuite = function(name){
this.name = name || 'Fuite';
this.caneat = function(){
console.log(this.name+":YES ");
}
}async

var Apple = function(name){
//apply修改this的指向
Fuite.apply(this,[name]);
}
var temp = function(){函數

}
temp.prototype = Fuite.prototype;
Apple.prototype = new temp();

var apple1 = new Apple("tom");
apple1.caneat();//tom:YES
console.log(apple1 instanceof Apple);//true
console.log(apple1 instanceof Fuite);//trueui

//3、閉包this

/**
* 閉包就是有權訪問另外一個函數做用域的變量的函數
*
* 閉包的優勢:防止數據污染
*
*
* 閉包的缺點:容易形成內存泄漏
*
**/
function outer(){
var name = "one";
var fn = function(){
console.log("內部function");
}url

var inner = function(){ return name; } return inner; } var result = outer(); console.log(result);

相關文章
相關標籤/搜索