1

How does one 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() {
kelloti
8,9596 gold badges52 silver badges86 bronze badges
asked Feb 22, 2011 at 0:43
0

2 Answers 2

2

You can write a jQuery plugin...

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

and then use it like:

$('#thingy').mainWindow();
kelloti
8,9596 gold badges52 silver badges86 bronze badges
answered Feb 22, 2011 at 0:45
Sign up to request clarification or add additional context in comments.

Comments

1

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/

answered Feb 22, 2011 at 0:47

2 Comments

Thanks for the reply. I'm using your suggestion to extend. How would i go about calling the bindAll function. I've tried $.MainWindow = bindAll() but to no avail.
@GregS7: assuming bindAll is a method off of MainWindow, $.MainWindow.bindAll()

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.