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 (){
    
  } }

相关文章:

  • 2022-12-23
  • 2022-02-23
  • 2022-02-25
  • 2022-12-23
  • 2022-02-07
猜你喜欢
  • 2021-10-10
  • 2022-01-11
  • 2021-11-20
  • 2021-08-09
  • 2021-11-01
  • 2021-06-20
相关资源
相似解决方案