1.1 underscore.jsÔ´Âë
Underscore.js ûÓжÔÔÉú JavaScript ¶ÔÏó½øÐÐÀ©Õ¹,¶øÊÇͨ¹ýµ÷Óà _() ·½·¨½øÐзâ×°£¬Ò»µ©·â×°Íê³É£¬ÔÉú JavaScript ¶ÔÏó±ã³ÉΪһ¸ö Underscore ¶ÔÏó¡£
1.1.1 Åжϸø¶¨±äÁ¿ÊÇ·ñÊǶÔÏó
// Is a given variable an object?
_.isObject = function(obj) {
var type = typeof obj;
return type === 'function' || type === 'object' && !!obj;
};
ÕâÊÇunderscore.jsµÄÅжϸø¶¨±äÁ¿ÊÇ·ñÊÇobjectµÄÒ»¶ÎÔ´Âë¡£ ÎÒÃÇÖªµÀtypeof»á·µ»ØÈçÏÂÁù¸öÖµ£º
1. 'undefined' --- Õâ¸öֵ䶨Ò壻2. 'boolean' --- Õâ¸öÖµÊDz¼¶ûÖµ£»3. 'string' --- Õâ¸öÖµÊÇ×Ö·û´®£»4. 'number' --- Õâ¸öÖµÊÇÊýÖµ£»5. 'object' --- Õâ¸öÖµÊǶÔÏó»ònull£»6. 'function' --- Õâ¸öÖµÊǺ¯Êý¡£
¶ø&&µÄÓÅÏȼ¶Òª¸ßÓë||¡£!!µÄ×÷ÓÃÏ൱ÓÚBoolean()£¬½«Æäת»»Îª²¼¶ûÖµ¡£
1.1.2 Åжϸø¶¨ÖµÊÇ·ñÊÇDOMÔªËØ
// Is a given value a DOM element?
_.isElement = function(obj) {
return !!(obj && obj.nodeType === 1);
};
ͬÑù!!Ï൱ÓÚBoolean()µÄ×÷Óã¬nodeType === 1Ôò˵Ã÷ÊÇÔªËØ½Úµã£¬ÊôÐÔattrÊÇ2 £¬Îı¾textÊÇ3
<body>
<p id="test">²âÊÔ</p><script>
var t = document.getElementById('test');
alert(t.nodeType);//1
alert(t.nodeName);//p
alert(t.nodeValue);//null</script></body>
firstChildÊôÐÔ
var t = document.getElementById('test').firstChild;
alert(t.nodeType);//3
alert(t.nodeName);//#test
alert(t.nodeValue);//²âÊÔ
Îı¾½ÚµãÒ²ËãÊÇÒ»¸ö½Úµã£¬ËùÒÔpµÄ×Ó½ÚµãÊÇÎı¾½Úµã£¬ËùÒÔ·µ»Ø3
1.2 zeptoÔ´Âë
1.2.1 ÅжÏÊÇ·ñÊÇÊý×é
isArray = Array.isArray ||
function(object){ return object instanceof Array }
Array.isArray() ·½·¨£ºÈç¹ûÒ»¸ö¶ÔÏóÊÇÊý×é¾Í·µ»Øtrue£¬Èç¹û²»ÊÇÔò·µ»Øfalse¡£
instanceof ÓÃÓÚÅжÏÒ»¸ö±äÁ¿ÊÇ·ñij¸ö¶ÔÏóµÄʵÀý£¬Èç
var a= [];
alert(a instanceof Array);//·µ»Ø true
ͬʱ alert(a instanceof Object) Ò²»á·µ»Ø true
isArray ·µ»Ø²¼¶ûÖµ£¬Èç¹ûArray.isArrayΪtrue£¬Ôò·µ»Øtrue£¬·ñÔò·µ»Øobject instanceof ArrayµÄ½á¹û¡£
1.2.2 Êý¾ÝÀàÐÍÅжÏ
class2type = {},
function type(obj) {
return obj == null ? String(obj) :
class2type[toString.call(obj)] || "object"
}
function isFunction(value) { return type(value) == "function" }
function isWindow(obj) { return obj != null && obj == obj.window }
function isDocument(obj) { return obj != null && obj.nodeType == obj.DOCUMENT_NODE }
function isObject(obj) { return type(obj) == "object" }
class2typeÊÇÒ»¸ö¿Õ¶ÔÏó£¬Êµ¼ÊÉÏÒ»¸öʲô¶¼Ã»ÓеĿնÔÏóÊÇÕâÑù´´½¨µÄObject.create(null);
ÎÒÃÇ¿ÉÒÔͨ¹ýObject.prototype.toString.call()·½·¨À´ÅжÏÊý¾ÝÀàÐÍ£¬ÀýÈ磺
console.log(Object.prototype.toString.call(123)) //[object Number] console.log(Object.prototype.toString.call('123')) //[object String] console.log(Object.prototype.toString.call(undefined)) //[object Undefined] console.log(Object.prototype.toString.call(true)) //[object Boolean] console.log(Object.prototype.toString.call({})) //[object Object] console.log(Object.prototype.toString.call([])) //[object Array] console.log(Object.prototype.toString.call(function(){})) //[object Function]
Ê×ÏÈÈç¹û²ÎÊýobjÊÇundefined»ònull£¬Ôòͨ¹ýString(obj)ת»»Îª¶ÔÓ¦µÄÔʼ×Ö·û´®“undefined”»ò“null”¡£
È»ºóclass2type[toString.call(obj)]Ê×ÏȽèÓÃObjectµÄÔÐÍ·½·¨toString()À´»ñÈ¡objµÄ×Ö·û´®±íʾ£¬·µ»ØÖµµÄÐÎʽÊÇ [object class]£¬ÆäÖеÄclassÊÇÄÚ²¿¶ÔÏóÀà¡£
È»ºó´Ó¶ÔÏóclass2typeÖÐÈ¡³ö[object class]¶ÔÓ¦µÄСд×Ö·û´®²¢·µ»Ø£»Èç¹ûδȡµ½ÔòÒ»ÂÉ·µ»Ø“object¡£
1.2.3 get·½·¨
get: function(idx){
return idx === undefined ? slice.call(this) : this[idx >= 0 ? idx : idx + this.length]
},
È¡¼¯ºÏÖжÔÓ¦Ö¸¶¨Ë÷ÒýµÄÖµ£¬Èç¹ûidxСÓÚ0,ÔòidxµÈÓÚidx+length,lengthΪ¼¯ºÏµÄ³¤¶È.
¿ÉÄÜÄã¸Õ¿´µ½slice.call(this)»á¾õµÃºÜÄÉÃÆ£¬Æäʵ²»½öÊÇzepto.jsµÄÔ´Â룬°üÀ¨jQuery£¬backboneµÄÔ´Âë¶¼ÊÇÕâôдµÄ£¬Ö»²»¹ýËüÃÇÔÚ×ͷ×öÁËÉùÃ÷£º
var push = array.push;var slice = array.slice;var splice = array.splice;
ËùÒÔslice.call(this)Æäʵ»¹ÊÇArray.slce.call(this)
±¾ÎİæÈ¨¹éÀÖÓã²¥¿ÍUIÅàѵ
ѧԺËùÓУ¬»¶Ó×ªÔØ£¬×ªÔØÇë×¢Ã÷×÷Õß³ö´¦¡£Ð»Ð»£¡
×÷ÕߣºÀÖÓã²¥¿ÍUIÅàѵѧԺ
Ê×·¢£º(F8H3IKU(@43DQ[32YWO.png)

http://www.itcast.cn/ui