firebug的一個有趣現象介紹

來源:互聯網
上載者:User

複製代碼 代碼如下:
var obj = {length:0,splice:function(){}}
console.log(obj)

猜猜上面會列印出啥?
沒錯,列印出來的看起來是一個空數組。。。
在FIREBUG裡如果一個對象同時擁有length屬性和splice方法,就會被firebug顯示為數組的形式。。。
如果以前注意過的話就會發現JQUERY就是這麼寫的,通過選取器列印出來的看起來跟數組一樣。
一直以來我都很好奇為毛返回的數組,但是卻有數組根本沒有的方法,也沒有數組該有的方法,比如:pop等
最後通過查資料才發現這麼一個有趣的現象。。
但是在IE下面列印出來的就是正常的[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方法的一個庫就誕生了。。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.