jQuery $.proxy() bind this

Description

jQuery $.proxy() bind this

View in separate window

<!DOCTYPE html>
<html>
<head>
<script 
 src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>/*from ww w.  ja  v a  2 s  . c  om*/
<script>
$(document).ready(function(){

test = function(){
  this.txt = "This is an object property";
  $("div").click($.proxy(this.myClick, this));
};

test.prototype.myClick = function(event){
   document.getElementById("demo").innerHTML = 
      this.txt +" " + event.currentTarget.nodeName;
};

var x = new test();

});
</script>
</head>
<body>

<p id="demo"></p>
<div>This is a div element.</div>

</body>
</html>



PreviousNext

Related