Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
391 views
in Technique[技术] by (71.8m points)

terminology - What is a jQuery Object?

JavaScript kind of redefines what an Array means because an array is an object with a .length property and methods like .slice() and .join().

jQuery defines a jQuery object as "Array like", because it has a length property but it doesn't have certain array methods like join().

If I were to define the jQuery object as an object, and forget about mentioning anything to do with an array, how would I define it? What properties does it have besides length?

I guess all the methods are what you see in the documentation, far exceeding the number of methods that are in an array.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

A jQuery object is array-like which means that it contains zero or more indexes (properties which names are positive integers starting with zero). Besides those indexes, a jQuery object contains these properties:

  • length
  • context
  • selector

And also around 140 inherited methods (which are defined on the jQuery.prototype object - you can do console.dir(jQuery.prototype) to get a full list... ).

Note that jQuery objects do not contain (or inherit) the Array methods (slice, substr, ...). If you want to execute those methods on your jQuery object, use call/apply.


For example, if you have 3 TEXTAREA elements on the page and you do this:

var j = $('textarea');

then this j jQuery object will contain these properties:

  • 0 - reference to the first TEXTAREA element
  • 1 - reference to the second TEXTAREA element
  • 2 - reference to the third TEXTAREA element
  • length - which is 3
  • context - reference to the document object
  • selector - which is 'textarea'
  • plus all those inherited methods...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...