We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
带Promise的话后续操作感觉更顺,请斧正:
ES6:
'use strict'; !function() { let JSONP = function(url, {data, callback} = {}) { let args = Object(data); let index = JSONP.counter++; let script = document.createElement('script'); let queryStrings = Object.getOwnPropertyNames(args) .map(key => key + '=' + encodeURIComponent(args[key])) .concat(['callback=' + encodeURIComponent(`JSONP[${index}]`)]); let promise = new Promise(function(resolve, reject) { Object.defineProperty(JSONP, index, { writable: false, configurable: true, value: resolve }); script.onerror = reject; }); let head = document.head; let onload = () => (script.onload = script.onreadystatechange = null, head.removeChild(script)); let scrUrl = String(url); script.src = scrUrl + (-1 === scrUrl.indexOf('?') ? '?' : '&') + queryStrings.join('&'); script.onload = onload; script.onreadystatechange = () => /loaded|complete/.test(script.readyState) && onload(); head.appendChild(script); return promise .then(response => (delete JSONP[index], response)) .then(response => 'function' === typeof callback ? callback(response) : response) .catch(error => console.warn(error)); }; JSONP.counter = 0; return window.JSONP = JSONP; }();
jQuery:
'use strict'; !function() { var JSONP = function(url, descriptor) { descriptor = Object(descriptor); url = String(url); var data = descriptor.data; var callback = descriptor.callback; var args = Object(data); var index = JSONP.counter++; var script = document.createElement('script'); var queryStrings = []; var promise = new jQuery.Deferred(); var head = document.head; var hasOwnProperty = Object.prototype.hasOwnProperty; var onload = function() { script.onload = script.onreadystatechange = null; return head.removeChild(script); }; for(var key in args) { if(hasOwnProperty.call(args, key)) { queryStrings.push(key + '=' + encodeURIComponent(args[key])); } } queryStrings.push('callback=' + encodeURIComponent('JSONP[' + index + ']')); JSONP[index] = function(response) { delete JSONP[index]; return promise.resolve('function' === typeof callback ? callback(response) : response); }; script.src = url + (-1 === url.indexOf('?') ? '?' : '&') + queryStrings.join('&'); script.onerror = promise.reject; script.onload = onload; script.onreadystatechange = function() { return /loaded|complete/.test(this.readyState) && onload(); }; head.appendChild(script); return promise.fail(function(error) { return console.warn(error); }); }; JSONP.counter = 0; return window.JSONP = JSONP; }();
The text was updated successfully, but these errors were encountered:
'use strict'; 写在应该写在函数内部
'use strict';
Sorry, something went wrong.
No branches or pull requests
带Promise的话后续操作感觉更顺,请斧正:
ES6:
jQuery:
The text was updated successfully, but these errors were encountered: