Am a little confused about the javascript object,say I have the DOM below
<div class='of'></div>
<div class='of'></div>
<div class='of'></div>
<div class='of'></div>
//select all of them
var fof = $('.of')
So fof is an array with 4 DOM object right? Can I treat the fof as javascript object ,and define some method like:
fof = {
open:function(){
}
}
And is it possible to define some ‘action’ for fof; like click dblclick, so when the ‘action’ happens (like a event handler),call some method! in steady of hard coding
fof.click(function(){
})
How can I do that?
Ok, first the basics. In Javascript, functions are first class objects. That means that you can do:
the same way you can do:
In Javascript, all true objects (pretty much everything except primitives) have properties. These properties can be set to anything that a variable can be set to:
Whenever you see a method being used on an object, you’re just seeing the function that was set as a property being run.
Since the jQuery results object (eg.
$('.of')), which by the way is more than just an array, is a Javascript object, it can have properties, and those properties can be functions. So you absolutely can do:Finally, you asked about making your own jQuery-like events. This is very easy to do using jQuery. Here’s a quick example: