js定義類方法的常用幾種定義
1 定義方法,方法中包含實現
function createCORSRequest() {
var xhr = new XMLHttpRequest();
xhr.onload = function () {
console.log('Response : ' + xdr.responseText);
};
xhr.onerror = function () {
console.log('Failed to retrieve data!');
};
return xhr;
}
調用
var xhr = createCORSRequest();
xhr.open('GET', 'https://www.hsbc.com.hk/zh-cn/index.html', true);
xhr.send(null);
2 定義對象,用對象擴展方法
var img = new Image();
img.onload = function () {
console.debug('request successfully.');
};
img.onerror = function () {
console.debug('request failed');
}
img.src = 'http://www.somesite.com/test?username=AndyLuo&ad=wealth';
3 定義類,用類實現方法
var Sign = {
_businessId: 0,
init: function () {
},
BindSignEvent: function (){
}
}