Home  >  Article  >  Web Front-end  >  Introduction to an interesting phenomenon of firebug_javascript skills

Introduction to an interesting phenomenon of firebug_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:59:09837browse
Copy code The code is as follows:

var obj = {length:0,splice:function(){} }
console.log(obj)

Guess what will be printed above?
Yes, what prints out looks like an empty array. . .
In FIREBUG, if an object has both the length attribute and the splice method, it will be displayed as an array by Firebug. . .
If you have paid attention before, you will find that JQUERY is written like this, and the printout through the selector looks the same as an array.
I have always been curious about the array returned by Mao, but there are methods that arrays do not have at all, and there are no methods that arrays should have, such as: pop, etc.
Finally, I found such an interesting phenomenon by checking the information. . .
But what is printed out under IE is normal [Object Object].
So you can play like this. . . .
Copy code The code is as follows:

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) {// Completely copy the merge method in jquery - -
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')

So, the copycat version of jquery only supports the tagName selector and only A library of copycat CSS methods was born. .
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn