Enforce the context of the "test" function - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:proxy

Description

Enforce the context of the "test" function

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){

test = function(){/*from  w w w .j  a  va2 s  . co m*/
    this.txt = "property";
    $("div").click($.proxy(this.myClick, this));
};

test.prototype.myClick = function(event){
    console.log(this.txt);
    console.log(event.currentTarget.nodeName);
};

var x = new test();

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

<div>This is a div element.</div>

</body>
</html>

Related Tutorials