开发者

passing javascript object to function in a jquery way?

Yeah so I've been messing around with javascript a while but only recently got into stuff like object orientation, prototyping and using objects for all functions and vars.

But, many 开发者_运维技巧frameworks like jQuery or extJS have something I have yet to grasp, you can define an object by using a built in function for searching the dom, example:

var a = $('#mydiv');

and then you can do a.click(function);

So my question is, how can I create a "framework" of sorts where I can write code in that style, like example:

var mydiv = document.querySelector('mydiv');
mydiv.neph(args,args);

So I have my object defined, in this case it's a dom element or whatever, now I pass it to my function "neph" with arguments, I want to create code that allows me to do this. But since mydiv does not have any function, it only has in this case the dom element right so mydiv.neph does not exist, but in jquery you can define whatever var and .click or .mouseover or whatever does exists within the object as functions? Confusion ensues! :D

Ok sorry if this is a retarded question btw =P


jQuery and other libraries define a function called $ that takes several optional parameters. The object returned by calling $ is not a DOM element, but a jQuery object wrapping a DOM element up with a set of convenient functions.

You can do something similar yourself:

<html>
   <body>
      <input id="derp" type="text"/>
<script type="text/javascript">

function $(id)
{
   return new myLibrary(id);
};

function myLibrary(id)
{
   this.el = document.getElementById(id);
};

myLibrary.prototype.help = function()
{
   alert(this.el.id);
   return this;
};

// Woah! My own fake jquery!
$("derp").help();
</script>

   </body>
</html>

jQuery is far more sophisticated, of course. For example, it will use apply and call to set this correctly in calls like jQuery.each.


You need to create a Prototype in javascript. This is what allows you to add a function to an object that's already defined (i.e. the .click() function that you gave as an example).

You can have a look at the jQuery code, it's open source. It's not the simplest code, but you can still see how it works and how they do it.


Mike's comment is a good answer: Look at jquery or Ext-Core's sources.

Maybe what you're missing is that, in jquery, for instance $() returns a jquery object, which wraps the plain vanilla DOM node, providing extended functionality.


In jQuery, $ is just an alias to the jQuery object. So when you call $('#mydiv'); you're really calling a function like jQuery('#mydiv'); So part of what makes jQuery so cool is that every the $() function returns this, which means when you call the $() you're getting a handle to the jQuery object and all of the methods it has on it. That is what allows you to do something like this:

var a = $('#mydiv');
a.click(function() { // etc etc etc });

So to pull off your example:

var mydiv = document.querySelector('mydiv');
mydiv.neph(args,args);

You'd have to create an object that has a function called neph on it and return that object in the context of mydiv when you call querySelector.

var myFramework = function() {
  this.context = undefined;
  this.neph = function(arg, args) {
    console.log(arg, args);
  }
};

var myCuteFunction = function(element) {
  var f = new myFramework();
  f.context = element;
  return f;
}

// ...

var x = myCuteFunction('#mydiv');
x.neph(arg, args);


Since nobody has really answered about Ext, you can easily extend the element wrapper prototype:

Ext.override(Ext.Element, {
    myMethod: function(a, b){
        console.log(a, b, this);
    }

});

"this" will refer to the Ext.Element object.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜