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

WBOY
Release: 2016-05-16 17:35:40
Original
1060 people have browsed it

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();


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template