Last active
October 17, 2018 21:35
-
-
Save ryanzhouff/0d30ba99d3594fb1ddc261ac30a30228 to your computer and use it in GitHub Desktop.
free-jQuery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// convert NodeList to array | |
function makeArray(nodes) { | |
var array = null; | |
var i = 0; | |
var length = nodes.length; | |
if (!nodes) return []; | |
try { | |
// for standard | |
array = [].slice.call(nodes, 0); | |
} catch(e) { // for ie8 | |
for (; i < length; i++) { | |
array.push(nodes[i]); | |
} | |
} | |
return array; | |
} | |
function extend(out) { | |
out = out || {}; | |
var obj = null; | |
for (var i = 0; i < arguments.length; i++) { | |
obj = arguments[i]; | |
if (!obj) { | |
continue; | |
} | |
for (var key in obj) { | |
if (obj.hasOwnProperty(key)) { | |
if (typeof obj[key] === "object") { | |
extend(out[key], obj[key]); | |
} else { | |
out[key] = obj[key]; | |
} | |
} | |
} | |
} | |
return out; | |
} | |
function slima(url, options) { | |
if (!url) { | |
throw new Error("请填写资源地址"); | |
} | |
options = options || {}; | |
var defaults = { | |
method: 'get', | |
success: function () {}, | |
error: function () {} | |
}; | |
var settings = extend({}, defaults, options); | |
console.log(settings) | |
var xhr = new XMLHttpRequest(); | |
xhr.onreadystatechange = function () { | |
if (xhr.readyState === 4) { | |
if (xhr.status >= 200 && xhr.status < 300) { | |
var response = ""; | |
var responseType = xhr.getResponseHeader("Content-Type"); | |
if (responseType.indexOf("xml") !== 1 && xhr.responseXML) { | |
response = xhr.responseXML; | |
} else if (responseType.indexOf("application/json") !== -1 && xhr.responseText) { | |
response = JSON.parse(xhr.responseText); | |
} else { | |
response = xhr.responseText; | |
} | |
settings.success && settings.success(response); | |
} else { | |
settings.error && settings.error(xhr, xhr.statusText); | |
} | |
} | |
}; | |
if (settings.method.toUpperCase() === "GET") { | |
xhr.open(settings.method.toUpperCase(), url, true); | |
xhr.send(null); | |
} | |
} | |
var getStyle = function (el, attr) { | |
// For IE | |
if (el.currentStyle) { | |
return el.currentStyle[attr]; | |
} else { | |
return getComputedStyle(el, false)[attr]; | |
} | |
}; | |
var toString = Object.prototype.toString; | |
/** | |
* Determine if a value is an Array | |
* @param {Object} val The value to set | |
* @returns {boolean} True if val is Array, otherwise false | |
*/ | |
function isArray(val) { | |
return toString.call(val) === '[object Array]'; | |
} | |
/** | |
* Determine if a value is an String | |
* @param {Object} val The value to set | |
* @returns {boolean} True if val is String, otherwise false | |
*/ | |
function isString(val) { | |
return typeof val === 'string'; | |
} | |
/** | |
* Determine if a value is an Number | |
* @param {Object} val The value to set | |
* @returns {boolean} True if val is Number, otherwise false | |
*/ | |
function isString(val) { | |
return typeof val === 'number'; | |
} | |
function isUndefined(val) { | |
return typeof val === 'undefined'; | |
} | |
function isObject(val) { | |
return val !== null && typeof val === 'object'; | |
} | |
function isDate(val) { | |
return toString.call(val) === '[object Date]'; | |
} | |
function isFile(val) { | |
return toString.call(val) === '[object File]'; | |
} | |
function isFunction(val) { | |
return toString.call(val) === '[object Function]'; | |
} | |
function isStream(val) { | |
return isObject(val) && isFunction(val.pipe); | |
} | |
// trime whitespace of the beginning and end of a string | |
function trim(str) { | |
return str.replace(/^\s*/, '').replace(/\s*$/, ''); | |
} | |
/** | |
* iterate over an array or object invoking a function each time. | |
*/ | |
function forEach(obj, fn) { | |
// just return if no value provided | |
if (obj === null || obj === 'undefined') { | |
return; | |
} | |
if (!isObject(obj)) { | |
obj = [obj]; | |
} | |
if (isArray(obj)) { | |
for (var i = 0, len = obj.length; i < len; i++) { | |
fn.call(null, obj[i], i, obj); | |
} | |
} else { | |
for (var key in obj) { | |
if (obj.hasOwnProperty(key)) { | |
fn.call(null, obj[key], key, obj); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment