js常用几种类方法实现

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 (){
    
  } }
posted @ 2016-02-02 18:25  不想当码农  阅读(857)  评论(0编辑  收藏  举报