开发者

build custom function based on prototype and jquery

开发者 https://www.devze.com 2023-03-30 19:38 出处:网络
I have used both protoype and jquery. Sometimes I have to build some custom functions,for exmaple,I want to make a common Search class,in prototype I may write it this way:

I have used both protoype and jquery.

Sometimes I have to build some custom functions,for exmaple,I want to make a common Search class,in prototype I may write it this way:

var Searcher=Class.create();
Searcher.prototype={
  initilize:function(xxx){},
  search:function(url,para){
    //here ,I may use some internal method,
    this._check(url,para)
  }
  _check:function(url,para){}
}

In the above code,in the method of "search",I need "_check" method which maybe used repeated. So I extra the codes to the function "_check".

However when I want to do the samething in jquery,I do not know how to do :

(function($){
  $.search=function(xxxx){
    //how about if I want to extra some common codes to a method,where to place it?

    //here?
    function _check(xxxx){}
  }

  //or here?
  $._check=function(xxxx) {}
})(JQuery)

It se开发者_开发问答ems that the prototype should be preferred when build custom util class,but I really like the dom operation manner such as the "chain operation",the "css",the ....

How do you guys do?


In jQuery, this is the usual pattern of private utility functions that your plug-in function may use:

(function($){
  $.search=function(xxxx){
    // You can use _check() here
  };

  function _check(xxxx) {
    // Do the work that _check does
  }
})(jQuery)

(Also note that the j in jQuery is in lower, not upper, case.)

Because I'm keen on named functions, I usually take it a step further:

(function($){
  $.search=search;

  function search(xxxx){
    // You can use _check() here; if you want to pass on `this`, either
    // do it as an argument or do this:  _check.call(this, xxxx);
  }

  function _check(xxxx) {
    // Do the work that _check does
  }
})(jQuery)

In both cases, your _check function is completely private to your plug-in, because it's scoped to the anonymous function you've used to wrap your plug-in.


Side note: Your Prototype example is out of date. That style of use of Class.create dates from v1.5. Since v1.6 was released (four years ago), you do this:

var Searcher=Class.create({
  initialize:function(xxx){},
  search:function(url,para){
    //here ,I may use some internal method,
    this._check(url,para)
  }
  _check:function(url,para){}
});

Note how I'm passing the object defining the methods for the prototype into Class.create, not replacing the prototype afterward. Prototype v1.6 and above do plumbing for you on the prototype that you lose if you replace it. It's probably also worth pointing out that your _check method is not private, anyone can call it. If you really want it to be private, you can use the same technique as above:

var Searcher = (function() {
  var rv = Class.create({
    initialize:function(xxx){},
    search:function(url,para){
      //here ,I may use some internal method,
      _check.call(this, url, para)
    }
  });

  function _check(url,para){}

  return rv;
})();

Note that the way you call _check changes. That's how you do it if you want this to mean the same thing in _check that it does in search, or you can just make it an argument you pass to _check.

0

精彩评论

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