Home >Web Front-end >JS Tutorial >js binding event this points to a changed problem solution_javascript skills

js binding event this points to a changed problem solution_javascript skills

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-05-16 17:35:401116browse

The function can be extended as follows

Copy the code The code is as follows:

Function.prototype.bind = function(obj) {
var _this = this;
return function() {
_this.apply(obj,arguments);
}
}

The usage is as follows
Copy code The code is as follows:

var a = function(){
alert(this.title)
}.bind(document);
a();

is commonly used here
Copy the code The code is as follows:

function myalert() {
this.title = 'hello world';
this.init = function() {
$("#xxx").click(this.close.bind(this));
}
this.close = function() {
alert(this.title)
}
}

Copy code The code is as follows:

var a = new myalert();

a.init();


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn