箭头函数 this arrow function 无this

 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

import wepy from 'wepy'
import util from './util'
import md5 from './md5'
// import tip from './tip'

const networkStatusChangeLog = () => {
  try {
    wx.removeStorageSync('onNetworkStatusChange')
  } catch (e) {
    // Do something when catch error
    console.log(e)
  }
  const log = new Date().toUTCString() + 'BizInfo' + 'uploadWhenOk'
  wx.setStorageSync('onNetworkStatusChange', log)
}

const isConnected = () => {
  let that = this
  wx.onNetworkStatusChange(function (res) {
    that.networkStatus = res
  })
  if (!this.networkStatus.isConnected) {
    wx.showToast({
      title: '无网络',
      icon: 'loading',
      duration: 2000
    })
    networkStatusChangeLog()
    return false
  }
  return true
}

const appendInfo = () => {
  const API_SECRET_KEY = 'https://github.com/dyq086/wepy-mall/tree/master/src'
  const TIMESTAMP = util.getCurrentTime()
  const SIGN = md5.hex_md5((TIMESTAMP + API_SECRET_KEY).toLowerCase())
  const MORE = 'more......'
  return {
    'API_SECRET_KEY': API_SECRET_KEY,
    'TIMESTAMP': TIMESTAMP,
    'SIGN': SIGN,
    'MORE': MORE
  }
}

const wxRequest = async (params = {}, url) => {
  console.log('wxRequest', params)
  const c = await isConnected()
  if (!c) {
    return
  }
  // tip.loading()
  let data = params.query || {}
  const header = params.header || {}
  const isAppend = params.isAppend || false
  if (isAppend) {
    const a = appendInfo()
    for (let k in a) {
      eval('data.' + k + '= a.' + k)
    }
  }
  let res = await wepy.request({
    url: url,
    method: params.method || 'GET',
    data: data,
    header: header
  })
  // tip.loaded()
  console.log('wxRequest', res)
  return res
}

module.exports = {
  wxRequest
}

  D:GPUGOMPwepympBMCwepysrcutilswxRequest.js

wepy build  --no-cache

D:GPUGOMPwepympBMCwepydistutilswxRequest.js

'use strict';

var _wepy = require('./../npm/wepy/lib/wepy.js');

var _wepy2 = _interopRequireDefault(_wepy);

var _util = require('./util.js');

var _util2 = _interopRequireDefault(_util);

var _md = require('./md5.js');

var _md2 = _interopRequireDefault(_md);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

// import tip from './tip'

var networkStatusChangeLog = function networkStatusChangeLog() {
  try {
    wx.removeStorageSync('onNetworkStatusChange');
  } catch (e) {
    // Do something when catch error
    console.log(e);
  }
  var log = new Date().toUTCString() + 'BizInfo' + 'uploadWhenOk';
  wx.setStorageSync('onNetworkStatusChange', log);
};

var isConnected = function isConnected() {
  var that = undefined;
  wx.onNetworkStatusChange(function (res) {
    that.networkStatus = res;
  });
  if (!undefined.networkStatus.isConnected) {
    wx.showToast({
      title: '无网络',
      icon: 'loading',
      duration: 2000
    });
    networkStatusChangeLog();
    return false;
  }
  return true;
};

var appendInfo = function appendInfo() {
  var API_SECRET_KEY = 'https://github.com/dyq086/wepy-mall/tree/master/src';
  var TIMESTAMP = _util2.default.getCurrentTime();
  var SIGN = _md2.default.hex_md5((TIMESTAMP + API_SECRET_KEY).toLowerCase());
  var MORE = 'more......';
  return {
    'API_SECRET_KEY': API_SECRET_KEY,
    'TIMESTAMP': TIMESTAMP,
    'SIGN': SIGN,
    'MORE': MORE
  };
};

var wxRequest = function () {
  var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
    var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    var url = arguments[1];
    var c, data, header, isAppend, a, k, res;
    return regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            console.log('wxRequest', params);
            _context.next = 3;
            return isConnected();

          case 3:
            c = _context.sent;

            if (c) {
              _context.next = 6;
              break;
            }

            return _context.abrupt('return');

          case 6:
            // tip.loading()
            data = params.query || {};
            header = params.header || {};
            isAppend = params.isAppend || false;

            if (isAppend) {
              a = appendInfo();

              for (k in a) {
                eval('data.' + k + '= a.' + k);
              }
            }
            _context.next = 12;
            return _wepy2.default.request({
              url: url,
              method: params.method || 'GET',
              data: data,
              header: header
            });

          case 12:
            res = _context.sent;

            // tip.loaded()
            console.log('wxRequest', res);
            return _context.abrupt('return', res);

          case 15:
          case 'end':
            return _context.stop();
        }
      }
    }, _callee, undefined);
  }));

  return function wxRequest() {
    return _ref.apply(this, arguments);
  };
}();

module.exports = {
  wxRequest: wxRequest
};
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW

  

 An arrow function expression has a shorter syntax than a function expression and does not have its own thisargumentssuper, or new.target. These function expressions are best suited for non-method functions, and they cannot be used as constructors.

'use strict';

var _wepy = require('./../npm/wepy/lib/wepy.js');

var _wepy2 = _interopRequireDefault(_wepy);

var _util = require('./util.js');

var _util2 = _interopRequireDefault(_util);

var _md = require('./md5.js');

var _md2 = _interopRequireDefault(_md);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

// import tip from './tip'

var networkStatusChangeLog = function networkStatusChangeLog() {
  try {
    wx.removeStorageSync('onNetworkStatusChange');
  } catch (e) {
    // Do something when catch error
    console.log(e);
  }
  var log = new Date().toUTCString() + 'BizInfo' + 'uploadWhenOk';
  wx.setStorageSync('onNetworkStatusChange', log);
};

var isConnected = function isConnected() {
  var that = this;
  wx.onNetworkStatusChange(function (res) {
    that.networkStatus = res;
  });
  if (!this.networkStatus.isConnected) {
    wx.showToast({
      title: '无网络',
      icon: 'loading',
      duration: 2000
    });
    networkStatusChangeLog();
    return false;
  }
  return true;
};

var appendInfo = function appendInfo() {
  var API_SECRET_KEY = 'https://github.com/dyq086/wepy-mall/tree/master/src';
  var TIMESTAMP = _util2.default.getCurrentTime();
  var SIGN = _md2.default.hex_md5((TIMESTAMP + API_SECRET_KEY).toLowerCase());
  var MORE = 'more......';
  return {
    'API_SECRET_KEY': API_SECRET_KEY,
    'TIMESTAMP': TIMESTAMP,
    'SIGN': SIGN,
    'MORE': MORE
  };
};

var wxRequest = function () {
  var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
    var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    var url = arguments[1];
    var c, data, header, isAppend, a, k, res;
    return regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            console.log('wxRequest', params);
            _context.next = 3;
            return isConnected();

          case 3:
            c = _context.sent;

            if (c) {
              _context.next = 6;
              break;
            }

            return _context.abrupt('return');

          case 6:
            // tip.loading()
            data = params.query || {};
            header = params.header || {};
            isAppend = params.isAppend || false;

            if (isAppend) {
              a = appendInfo();

              for (k in a) {
                eval('data.' + k + '= a.' + k);
              }
            }
            _context.next = 12;
            return _wepy2.default.request({
              url: url,
              method: params.method || 'GET',
              data: data,
              header: header
            });

          case 12:
            res = _context.sent;

            // tip.loaded()
            console.log('wxRequest', res);
            return _context.abrupt('return', res);

          case 15:
          case 'end':
            return _context.stop();
        }
      }
    }, _callee, undefined);
  }));

  return function wxRequest() {
    return _ref.apply(this, arguments);
  };
}();

module.exports = {
  wxRequest: wxRequest
};
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW

  

import wepy from 'wepy'
import util from './util'
import md5 from './md5'
// import tip from './tip'

const networkStatusChangeLog = () => {
  try {
    wx.removeStorageSync('onNetworkStatusChange')
  } catch (e) {
    // Do something when catch error
    console.log(e)
  }
  const log = new Date().toUTCString() + 'BizInfo' + 'uploadWhenOk'
  wx.setStorageSync('onNetworkStatusChange', log)
}

const isConnected = function () {
  let that = this
  wx.onNetworkStatusChange(function (res) {
    that.networkStatus = res
  })
  if (!this.networkStatus.isConnected) {
    wx.showToast({
      title: '无网络',
      icon: 'loading',
      duration: 2000
    })
    networkStatusChangeLog()
    return false
  }
  return true
}

const appendInfo = () => {
  const API_SECRET_KEY = 'https://github.com/dyq086/wepy-mall/tree/master/src'
  const TIMESTAMP = util.getCurrentTime()
  const SIGN = md5.hex_md5((TIMESTAMP + API_SECRET_KEY).toLowerCase())
  const MORE = 'more......'
  return {
    'API_SECRET_KEY': API_SECRET_KEY,
    'TIMESTAMP': TIMESTAMP,
    'SIGN': SIGN,
    'MORE': MORE
  }
}

const wxRequest = async (params = {}, url) => {
  console.log('wxRequest', params)
  const c = await isConnected()
  if (!c) {
    return
  }
  // tip.loading()
  let data = params.query || {}
  const header = params.header || {}
  const isAppend = params.isAppend || false
  if (isAppend) {
    const a = appendInfo()
    for (let k in a) {
      eval('data.' + k + '= a.' + k)
    }
  }
  let res = await wepy.request({
    url: url,
    method: params.method || 'GET',
    data: data,
    header: header
  })
  // tip.loaded()
  console.log('wxRequest', res)
  return res
}

module.exports = {
  wxRequest
}

  

import wepy from 'wepy'
import util from './util'
import md5 from './md5'
// import tip from './tip'

const networkStatusChangeLog = () => {
  try {
    wx.removeStorageSync('onNetworkStatusChange')
  } catch (e) {
    // Do something when catch error
    console.log(e)
  }
  const log = new Date().toUTCString() + 'BizInfo' + 'uploadWhenOk'
  wx.setStorageSync('onNetworkStatusChange', log)
}

const isConnected = async function () {
  let that = this
  await wx.onNetworkStatusChange(function (res) {
    that.networkStatus = res
  })
  if (!this.networkStatus.isConnected) {
    wx.showToast({
      title: '无网络',
      icon: 'loading',
      duration: 2000
    })
    networkStatusChangeLog()
    return false
  }
  return true
}

const appendInfo = () => {
  const API_SECRET_KEY = 'https://github.com/dyq086/wepy-mall/tree/master/src'
  const TIMESTAMP = util.getCurrentTime()
  const SIGN = md5.hex_md5((TIMESTAMP + API_SECRET_KEY).toLowerCase())
  const MORE = 'more......'
  return {
    'API_SECRET_KEY': API_SECRET_KEY,
    'TIMESTAMP': TIMESTAMP,
    'SIGN': SIGN,
    'MORE': MORE
  }
}

const wxRequest = async (params = {}, url) => {
  console.log('wxRequest', params)
  const c = await isConnected()
  if (!c) {
    return
  }
  // tip.loading()
  let data = params.query || {}
  const header = params.header || {}
  const isAppend = params.isAppend || false
  if (isAppend) {
    const a = appendInfo()
    for (let k in a) {
      eval('data.' + k + '= a.' + k)
    }
  }
  let res = await wepy.request({
    url: url,
    method: params.method || 'GET',
    data: data,
    header: header
  })
  // tip.loaded()
  console.log('wxRequest', res)
  return res
}

module.exports = {
  wxRequest
}

  

'use strict';

var _wepy = require('./../npm/wepy/lib/wepy.js');

var _wepy2 = _interopRequireDefault(_wepy);

var _util = require('./util.js');

var _util2 = _interopRequireDefault(_util);

var _md = require('./md5.js');

var _md2 = _interopRequireDefault(_md);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

// import tip from './tip'

var networkStatusChangeLog = function networkStatusChangeLog() {
  try {
    wx.removeStorageSync('onNetworkStatusChange');
  } catch (e) {
    // Do something when catch error
    console.log(e);
  }
  var log = new Date().toUTCString() + 'BizInfo' + 'uploadWhenOk';
  wx.setStorageSync('onNetworkStatusChange', log);
};

var isConnected = function () {
  var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
    var that;
    return regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            that = this;
            _context.next = 3;
            return wx.onNetworkStatusChange(function (res) {
              that.networkStatus = res;
            });

          case 3:
            if (this.networkStatus.isConnected) {
              _context.next = 7;
              break;
            }

            wx.showToast({
              title: '无网络',
              icon: 'loading',
              duration: 2000
            });
            networkStatusChangeLog();
            return _context.abrupt('return', false);

          case 7:
            return _context.abrupt('return', true);

          case 8:
          case 'end':
            return _context.stop();
        }
      }
    }, _callee, this);
  }));

  return function isConnected() {
    return _ref.apply(this, arguments);
  };
}();

var appendInfo = function appendInfo() {
  var API_SECRET_KEY = 'https://github.com/dyq086/wepy-mall/tree/master/src';
  var TIMESTAMP = _util2.default.getCurrentTime();
  var SIGN = _md2.default.hex_md5((TIMESTAMP + API_SECRET_KEY).toLowerCase());
  var MORE = 'more......';
  return {
    'API_SECRET_KEY': API_SECRET_KEY,
    'TIMESTAMP': TIMESTAMP,
    'SIGN': SIGN,
    'MORE': MORE
  };
};

var wxRequest = function () {
  var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() {
    var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    var url = arguments[1];
    var c, data, header, isAppend, a, k, res;
    return regeneratorRuntime.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            console.log('wxRequest', params);
            _context2.next = 3;
            return isConnected();

          case 3:
            c = _context2.sent;

            if (c) {
              _context2.next = 6;
              break;
            }

            return _context2.abrupt('return');

          case 6:
            // tip.loading()
            data = params.query || {};
            header = params.header || {};
            isAppend = params.isAppend || false;

            if (isAppend) {
              a = appendInfo();

              for (k in a) {
                eval('data.' + k + '= a.' + k);
              }
            }
            _context2.next = 12;
            return _wepy2.default.request({
              url: url,
              method: params.method || 'GET',
              data: data,
              header: header
            });

          case 12:
            res = _context2.sent;

            // tip.loaded()
            console.log('wxRequest', res);
            return _context2.abrupt('return', res);

          case 15:
          case 'end':
            return _context2.stop();
        }
      }
    }, _callee2, undefined);
  }));

  return function wxRequest() {
    return _ref2.apply(this, arguments);
  };
}();

module.exports = {
  wxRequest: wxRequest
};
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2V

  

原文地址:https://www.cnblogs.com/rsapaper/p/9609595.html