firebug的一个有趣现象
生活随笔
收集整理的這篇文章主要介紹了
firebug的一个有趣现象
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
firebug的一個(gè)有趣現(xiàn)象 var obj = {length:0,splice:function(){}}
console.log(obj)
var splice = Array.prototype.splice;
var a = function() {
var a = function(name) {
return new a.fn.init(name)
}
a.fn = a.prototype;
a.fn.init = function(name) {
var arr = document.getElementsByTagName(name);
merge(this,arr);
}
a.fn.splice = splice;
a.fn.init.prototype = a.fn;
return a;
}()
function merge(first, second) {//完全抄襲jquery中的merge方法- -
var i = first.length || 0, j = 0;
if( typeof second.length === "number") {
for(var l = second.length; j < l; j++) {
first[i++] = second[j];
}
} else {
while(second[j] !== undefined) {
first[i++] = second[j++];
}
}
first.length = i;
return first;
}
a.fn.css = function(pop, val) {
for(var i = 0; i < this.length; i++) {
if(this[i].nodeType===1){
this[i].style[pop] = val;
}
}
return this;
}
var ab = a('div');
ab.css('backgroundColor', '#444444').css('borderWidth', '2px').css('borderStyle', 'solid')
console.log(obj)
?猜猜上面會(huì)打印出啥?
?
沒(méi)錯(cuò),打印出來(lái)的看起來(lái)是一個(gè)空數(shù)組。。。
在FIREBUG里如果一個(gè)對(duì)象同時(shí)擁有l(wèi)ength屬性和splice方法,就會(huì)被firebug顯示為數(shù)組的形式。。。
?
如果以前注意過(guò)的話(huà)就會(huì)發(fā)現(xiàn)JQUERY就是這么寫(xiě)的,通過(guò)選擇器打印出來(lái)的看起來(lái)跟數(shù)組一樣。
一直以來(lái)我都很好奇為毛返回的數(shù)組,但是卻有數(shù)組根本沒(méi)有的方法,也沒(méi)有數(shù)組該有的方法,比如:pop等
最后通過(guò)查資料才發(fā)現(xiàn)這么一個(gè)有趣的現(xiàn)象。。
但是在IE下面打印出來(lái)的就是正常的[Object Object]了。
于是就可以這么玩一下。。。。
var push = Array.prototype.push;var splice = Array.prototype.splice;
var a = function() {
var a = function(name) {
return new a.fn.init(name)
}
a.fn = a.prototype;
a.fn.init = function(name) {
var arr = document.getElementsByTagName(name);
merge(this,arr);
}
a.fn.splice = splice;
a.fn.init.prototype = a.fn;
return a;
}()
function merge(first, second) {//完全抄襲jquery中的merge方法- -
var i = first.length || 0, j = 0;
if( typeof second.length === "number") {
for(var l = second.length; j < l; j++) {
first[i++] = second[j];
}
} else {
while(second[j] !== undefined) {
first[i++] = second[j++];
}
}
first.length = i;
return first;
}
a.fn.css = function(pop, val) {
for(var i = 0; i < this.length; i++) {
if(this[i].nodeType===1){
this[i].style[pop] = val;
}
}
return this;
}
var ab = a('div');
ab.css('backgroundColor', '#444444').css('borderWidth', '2px').css('borderStyle', 'solid')
于是,山寨版jquery并且只支持tagName選擇器并且只有山寨CSS方法的一個(gè)庫(kù)就誕生了。。
posted on 2011-11-28 15:36 王廁所 閱讀(...) 評(píng)論(...) 編輯 收藏轉(zhuǎn)載于:https://www.cnblogs.com/wangwc/archive/2011/11/28/2266286.html
總結(jié)
以上是生活随笔為你收集整理的firebug的一个有趣现象的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: cadence -- FPM0.0.8.
- 下一篇: 如何在 DB2 Universal Da