开发者

Prototype into jquery

How does one go开发者_Go百科 about changing an existing prototype function into a jquery one?

ie.

MainWindow = function()
{
    this.activeUser = "";    
    this.name = "";
}

And the call to bindAll

MainWindow.prototype.bindAll = function() {


You can write a jQuery plugin...

(function($) {

   $.fn.mainWindow = function() {
      ...
   }

})(jQuery);

and then use it like:

$('#thingy').mainWindow();


The common method is to use an anonymous function in the context of jQuery, such as:

// anonymous function that is executed within the jQuery context
// to preserve reference in case of $.noConflict
(function($){
    // $ is now short for jQuery
    // $.fn is short for jQuery.prototype

    // if you want $.myCustomFunction
    $.extend({
        myCustomFunction: function(arg){
            $('#test').append($('<p>').text('myCustomFunction: '+arg));
        }
    });

    // or if you want $('...').myCustomFunction()
    $.fn.extend({
        myCustomFunction: function(arg){
            $.myCustomFunction(arg + ' [from selector]');
        }
    });
})(jQuery);

Demo can be found here: http://jsfiddle.net/bradchristie/Cfsb2/2/

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜