function Loader() { this.load = function() { $.ajax({ url: '/test.php', success: function(data, textStatus, jqXHR) { // console.log(this); // this jQuery, // success } }); } } (new Loader()).load();
function cb(object, fnc) { return function() { return fnc.apply(object, arguments); } }
function Loader() { this.load = function() { $.ajax({ url: '/test.php', success: cb(this, this.onLoad) }) } this.onLoad = function(data, textStatus, jqXHR) { console.log(this); // this Loader } } (new Loader()).load();
cb(this, function(data, textStatus, jqXHR) { console.log(this); });
function Button() { this.render = function() { var submit = $('<button>').html('click me'); submit.bind('click', function(event) { // this HTML-, console.log(this); }); submit.appendTo($('body')); } } (new Button()).render();
function cb(object, fnc) { return function() { var args = [this]; for (var i in arguments) args.push(arguments[i]); return fnc.apply(object, args); } }
function Button() { this.render = function() { var submit = $('<button>').html('click me'); submit.bind('click', cb(this, this.onClick)); submit.appendTo($('body')); } this.onClick = function(target, event) { console.log(this); // this Button console.log(target); // HTML-, } } (new Button()).render();
Source: https://habr.com/ru/post/141463/
All Articles