当在console.log中调用时,jQuery实例如何显示为数组? [英] How does a jQuery instance appear as an array when called in console.log?
本文介绍了当在console.log中调用时,jQuery实例如何显示为数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当输入到JavaScript控制台时,jQuery对象显示为数组。但是,它仍然是jQuery对象的实例。
When entered into a JavaScript console, a jQuery object appears as an array. However, it's still an instance of the jQuery object.
var j = jQuery();
=> []
console.log(j);
=> []
console.log('test with string concat: ' + j);
=> test with string concat: [object Object]
j instanceof Array
=> false
j instanceof jQuery
=> true
如何用自己的对象复制这个?
How could one duplicate this with their own object?
--------- EDIT ---------
--------- EDIT ---------
感谢ZER0的支持。下面是一些示例代码来创建一个在控制台中像jQuery一样工作的对象:
Thanks to ZER0 for figuring it out. Here's some example code to create an object that works just like jQuery in the console:
var Foo = function() {
this.splice = Array.prototype.splice;
Array.prototype.push.apply(this, arguments);
return this;
}
var f = new Foo();
=> []
console.log(f);
=> []
console.log('test with string concat: ' + f);
=> test with string concat: [object Object]
f instanceof Array
=> false
f instanceof Foo
=> true
很酷。
推荐答案
我相信他们有这样的东西:
I believe they have something like that:
// Adding items to an object like an Array:
var myObject = {splice : Array.prototype.splice};
Array.prototype.push.call(myObject, 1, 10, "foo");
// Getting items from an object like an Array:
alert(Array.prototype.slice.call(myObject, 0));
console.log(myObject);
alert(myObject);
这篇关于当在console.log中调用时,jQuery实例如何显示为数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文