jQuery $.proxy() Method

jQueryWeb DevelopmentFront End Technology

The $.proxy() method in jQuery is used to take a function and returns a new one that will always have a particular context.


The syntax is as follows −


Above, the context parameter is the name of object where the function can be found, whereas the name parameter is the existing function whose context will be changed


Let us now see an example to implement the jQuery $.proxy() method −

<!DOCTYPE html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
      demo = function(){
         this.txt = "Object property!";
         $("p").click($.proxy(this.myClick, this));
      demo.prototype.myClick = function(event){
      var a = new demo();
<h1>Demo Heading</h1>
<p>p element with demo text. Click it!</p>


This will produce the following output −

Click on the p element to generate the following alert boxes −

Now, click on OK above −

Published on 11-Nov-2019 16:58:35