(function(winsow) {
// (Array)方法
var deletedIds = [];
var document = window.document;
// Array.prototype.slice
var slice = deletedIds.slice;
// Array.prototype.concat
var concat = deletedIds.concat;
// Array.prototype.push
var push = deletedIds.push;
// Array.prototype.indexOf
var indexOf = deletedIds.indexOf;
var class2type = {};
var toString = class2type.toString;
var hasOwn = class2type.hasOwnProperty;
var support = {};
var version = "1";
/* ============================== ============================== ============== */
function xQuery(domList) {
debugger;
// The jQuery object is actually just the init constructor 'enhanced'
// Need init if jQuery is called (just allow error to be thrown if not included)
return new xQuery.fn.init(domList);
};
// 核心工具,對外API
xQuery.fn = xQuery.prototype = {
each: function(callback) {
return xQuery.each(this, callback);
}
};
xQuery.fn.init = init;
/**
* 核心包裹
*
* @param {any} domList
* @returns
*/
function init(domList) {
if (typeof domList.length === 'number') {
for (var i = 0; i < domList.length; i++) {
this[i] = domList[i];
}
this.length = domList.length;
} else {
this[0] = domList;
this.length = 1;
}
return this;
};
init.prototype = xQuery.fn;
/* ============================== ============================== ============== */
// (jquery)擴充的方法
xQuery.extend = xQuery.fn.extend = function() {
var src, copyIsArray, copy, name, options, clone,
target = arguments[0] || {},
i = 1,
length = arguments.length,
deep = false;
// Handle a deep copy situation
if (typeof target === "boolean") {
deep = target;
// skip the boolean and the target
target = arguments[i] || {};
i++;
}
// Handle case when target is a string or something (possible in deep copy)
if (typeof target !== "object" && typeof target !== 'function') {
target = {};
}
// extend jQuery itself if only one argument is passed
if (i === length) {
target = this;
i--;
}
for (; i < length; i++) {
// Only deal with non-null/undefined values
if ((options = arguments[i]) != null) {
// Extend the base object
for (name in options) {
src = target[name];
copy = options[name];
// Prevent never-ending loop
if (target === copy) {
continue;
}
// Recurse if we're merging plain objects or arrays
if (deep && copy && (xQuery.isPlainObject(copy) ||
(copyIsArray = Array.isArray(copy)))) {
if (copyIsArray) {
copyIsArray = false;
clone = src && Array.isArray(src) ? src : [];
} else {
clone = src && xQuery.isPlainObject(src) ? src : {};
}
// Never move original objects, clone them
target[name] = xQuery.extend(deep, clone, copy);
// Don't bring in undefined values
} else if (copy !== undefined) {
target[name] = copy;
}
}
}
}
// Return the modified object
return target;
};
/* ============================== ============================== ============== */
/**
* 核心方法
*/
/**
* 核心方法
*/
xQuery.extend({
// Unique for each copy of jQuery on the page
expando: "xQuery" + (version + Math.random()).replace(/\D/g, ""),
// Assume jQuery is ready without the ready module
isReady: true,
error: function(msg) {
throw new Error(msg);
},
noop: function() {},
// See test/unit/core.js for details concerning isFunction.
// Since version 1.3, DOM methods and functions like alert
// aren't supported. They return false on IE (#2968).
isFunction: function(obj) {
return xQuery.type(obj) === "function";
},
isArray: Array.isArray || function(obj) {
return xQuery.type(obj) === "array";
},
isArrayLike: isArrayLike,
isWindow: function(obj) {
/* jshint eqeqeq: false */
return obj != null && obj == obj.window;
},
isNumeric: function(obj) {
// parseFloat NaNs numeric-cast false positives (null|true|false|"")
// ...but misinterprets leading-number strings, particularly hex literals ("0x...")
// subtraction forces infinities to NaN
// adding 1 corrects loss of precision from parseFloat (#15100)
var realStringObj = obj && obj.toString();
return !Array.isArray(obj) && (realStringObj - parseFloat(realStringObj) + 1) >= 0;
},
isEmptyObject: function(obj) {
var name;
for (name in obj) {
return false;
}
return true;
},
isPlainObject: function(obj) {
var key;
// Must be an Object.
// Because of IE, we also have to check the presence of the constructor property.
// Make sure that DOM nodes and window objects don't pass through, as well
if (!obj || xQuery.type(obj) !== "object" || obj.nodeType || xQuery.isWindow(obj)) {
return false;
}
try {
// Not own constructor property must be Object
if (obj.constructor &&
!hasOwn.call(obj, "constructor") &&
!hasOwn.call(obj.constructor. prototype, "isPrototypeOf")) {
return false;
}
} catch (e) {
// IE8,9 Will throw exceptions on certain host objects #9897
return false;
}
// Support: IE<9
// Handle iteration over inherited properties before own properties.
if (!support.ownFirst) {
for (key in obj) {
return hasOwn.call(obj, key);
}
}
// Own properties are enumerated firstly, so to speed up,
// if last one is own, then all properties are own.
for (key in obj) {}
return key === undefined || hasOwn.call(obj, key);
},
type: function(obj) {
if (obj == null) {
return obj + "";
}
return typeof obj === "object" || typeof obj === "function" ?
class2type[toString.call(obj)] || "object" :
typeof obj;
},
// Workarounds based on findings by Jim Driscoll
globalEval: function(data) {
if (data && xQuery.trim(data)) {
// We use execScript on Internet Explorer
// We use an anonymous function so that context is window
// rather than jQuery in Firefox
(window.execScript || function(data) {
window["eval"].call(window, data); // jscs:ignore requireDotNotation
})(data);
}
},
// Convert dashed to camelCase; used by the css and data modules
// Microsoft forgot to hump their vendor prefix (#9572)
camelCase: function(string) {
return string.replace(rmsPrefix, "ms-").replace(rdashAlpha, fcamelCase);
},
nodeName: function(elem, name) {
return elem.nodeName && elem.nodeName.toLowerCase() === name.toLowerCase();
},
// important
each: function(obj, callback) {
var length, i = 0;
if (isArrayLike(obj)) {
length = obj.length;
for (; i < length; i++) {
if (callback.call(obj[i], i, obj[i]) === false) {
break;
}
}
} else {
for (i in obj) {
if (callback.call(obj[i], i, obj[i]) === false) {
break;
}
}
}
return obj;
},
// Support: Android<4.1, IE<9
trim: function(text) {
return text == null ?
"" :
(text + "").replace(rtrim, "");
},
// results is for internal usage only
makeArray: function(arr, results) {
var ret = results || [];
if (arr != null) {
if (isArrayLike(Object(arr))) {
xQuery.merge(ret,
typeof arr === "string" ? [arr] : arr
);
} else {
push.call(ret, arr);
}
}
return ret;
},
inArray: function(elem, arr, i) {
var len;
if (arr) {
if (indexOf) {
return indexOf.call(arr, elem, i);
}
len = arr.length;
i = i ? i < 0 ? Math.max(0, len + i) : i : 0;
for (; i < len; i++) {
// Skip accessing in sparse arrays
if (i in arr && arr[i] === elem) {
return i;
}
}
}
return -1;
},
merge: function(first, second) {
debugger;
var len = +second.length,
j = 0,
i = first.length;
while (j < len) {
first[i++] = second[j++];
}
// Support: IE<9
// Workaround casting of .length to NaN on otherwise arraylike objects (e.g., NodeLists)
if (len !== len) {
while (second[j] !== undefined) {
first[i++] = second[j++];
}
}
first.length = i;
return first;
},
grep: function(elems, callback, invert) {
var callbackInverse,
matches = [],
i = 0,
length = elems.length,
callbackExpect = !invert;
// Go through the array, only saving the items
// that pass the validator function
for (; i < length; i++) {
callbackInverse = !callback(elems[i], i);
if (callbackInverse !== callbackExpect) {
matches.push(elems[i]);
}
}
return matches;
},
// arg is for internal usage only
map: function(elems, callback, arg) {
debugger;
var length, value,
i = 0,
ret = [];
// Go through the array, translating each of the items to their new values
if (isArrayLike(elems)) {
length = elems.length;
for (; i < length; i++) {
value = callback(elems[i], i, arg);
if (value != null) {
ret.push(value);
}
}
// Go through every key on the object,
} else {
for (i in elems) {
value = callback(elems[i], i, arg);
if (value != null) {
ret.push(value);
}
}
}
// Flatten any nested arrays
return concat.apply([], ret);
},
// A global GUID counter for objects
guid: 1,
// Bind a function to a context, optionally partially applying any
// arguments.
proxy: function(fn, context) {
var args, proxy, tmp;
if (typeof context === "string") {
tmp = fn[context];
context = fn;
fn = tmp;
}
// Quick check to determine if target is callable, in the spec
// this throws a TypeError, but we will just return undefined.
if (typeof fn !== 'function') {
return undefined;
}
// Simulated bind
args = slice.call(arguments, 2);
proxy = function() {
return fn.apply(context || this, args.concat(slice.call( arguments)));
};
// Set the guid of unique handler to the same of original handler, so it can be removed
proxy.guid = fn.guid = fn.guid || xQuery.guid++;
return proxy;
},
now: function() {
return +(new Date());
},
// jQuery.support is not used in Core but other projects attach their
// properties to it so it needs to exist.
support: support
});
/* ============================== ============================== ============== */
// Populate the class2type map
xQuery.each("Boolean Number String Function Array Date RegExp Object Error Symbol".split(" "),
function(i, name) {
class2type["[object " + name + "]"] = name.toLowerCase();
});
function isArrayLike(obj) {
debugger;
// Support: iOS 8.2 (not reproducible in simulator)
// `in` check used to prevent JIT error (gh-2145)
// hasOwn isn't used here due to false negatives
// regarding Nodelist length in IE
var length = !!obj && "length" in obj && obj.length,
type = xQuery.type(obj);
if (type === "function" || xQuery.isWindow(obj)) {
return false;
}
return type === "array" || length === 0 ||
typeof length === "number" && length > 0 && (length - 1) in obj;
}
window.xQuery = xQuery;
})(window)
沒有留言:
張貼留言