"use strict";
function ask(question, answer, ok, fail) {
var result = prompt(question, ");
if (result.toLowerCase() == answer.toLowerCase()) ok();
else fail();
}
var user = {
login: 'Vasily',
password: '12345',
// method to call from ask
loginDone: function(result) {
console.log( this.login + (result ? 'entered the site' : 'login failed') );
},
checkPassword: function() {
ask("Your password?" this.password
this.loginDone.bind(this, true)
,
this.loginDone.call(this, false)
);
}
};
var vasya = user;
user = null;
vasya.checkPassword();
Namely how exactly is the loss of context?
var foo = {
bar: function () {
console.log(this);
}
};
var bar = { bar: foo.bar };
foo.bar(); // call the function in the context of foo.
bar.bar(); // call the function but in the context bar
Find more questions by tags JavaScript
And please explain why the won't work if you just explicitly specify the object, passing in like so: user.loginDone(true/false)? - Howell commented on July 9th 19 at 10:09