Quick Reference
The $.proxy() method takes an existing function and returns a new one with a particular context.
Method 1:
// testing is the existing function to be called
testing = function() {
this.txt = 'This is the object property:';
$('div').click($.proxy(this.myClick, this));
};
testing.prototype.myClick = function(event) {
alert(this.txt + ' ' + event.currentTarget.nodeName);
};
var x = new testing();
Method 2:
var objPerson = {
name: 'Frank Pug',
age: 18,
new: function() {
$('p').after('Name: ' + this.name + '<br> Age: ' + this.age);
}
};
// objPerson is the "context" (name of the object)
// new is the "name" (object property to be changed)
$('button').click($.proxy(objPerson, 'new'));
Syntax
$(selector).proxy(function,context)
// or
$(selector).proxy(context,name)
Note
It is generally good practice to place your jQuery code/function inside the document load function so that the action takes place ONLY after the document has finished loading. This ensures that all of the page elements that you may be selecting are in place before running the code on them.
Parameters
Parameter | Description |
---|---|
function | The existing function to be called |
context | The name of the object where the function lies |
name | The existing function whose context will be changed (should be a property of the context object) |
jQuery Notes:
- To use jQuery on your site, it must first be downloaded from the official jQuery site and linked to in your document <head>, or linked to via a CDN in your document <head>
- It is generally good practice to place your jQuery code/function inside the document load function so that the action takes place ONLY after the document has finished loading
- When using jQuery, single or double quotation marks are acceptable and work identically to one another; choose whichever you prefer, and stay consistent
We’d like to acknowledge that we learned a great deal of our coding from W3Schools and TutorialsPoint, borrowing heavily from their teaching process and excellent code examples. We highly recommend both sites to deepen your experience, and further your coding journey. We’re just hitting the basics here at 1SMARTchicken.