A somewhat different version of ErikE's fourth suggestion that does much the same job but with what I think is simpler code is
function Person() {
this.speech = null;
}
Person.prototype.tryToSpeak = function () {
this.speech = "hello";
var person = this;
window.setTimeout(function() {person.speak();}, 1000);
};
Person.prototype.speak = function () {
console.log(this.speech);
};
var person = new Person();
person.tryToSpeak();
As Erik said, it's not really clear if you even need multiple Person objects, but if you do, this sort of technique might be simplest.
UPDATE (based on ErikE's comments): This version adds a name to the Person, and passes the speech in as a parameter to make it clear exactly who is saying what. The basic structure remains the same.
function Person(name) {
this.name = name;
}
Person.prototype.tryToSpeak = function (speech) {
var person = this;
window.setTimeout(function() {person.speak(speech);}, 1000);
};
Person.prototype.speak = function (speech) {
console.log(this.name + " says, '" + speech + "'.");
};
var lincoln = new Person("Abraham Lincoln");
var churchill = new Person("Winston Churchill");
lincoln.tryToSpeak("Four score and seven years ago...");
churchill.tryToSpeak("Never, never, never give up.");
The main point is that the person variable in tryToSpeak is a local variable stored in a closure and not a reference to some singleton.
thisvalue in JavaScript is very dynamic. Its value is based on how the function is called, not where the function was defined.