下記のコードからsaluteFriendsとsayHelloLaterメソッドにアクセスする際に助けが必要です。私はメソッドのスコープの間で混乱してきています。どちらかのメソッドがプライベートなのでアクセスできないので推測する。JavaScript - 関数にアクセスできません
function Person(name, friends) {
// friends is a list of strings
var say = function (sentence) {
console.log(name + ' says: ' + sentence);
};
Person.prototype.sayHello = function (otherName) {
this.say('hello ' + otherName + '!');
};
this.saluteFriends = function() {
friends.forEach(function (friend) {
sayHello(friend);
});
};
this.sayHelloLater = function (delay, otherName) {
setTimeout(function() {
this.sayHello(otherName);
}, delay);
};
}
var frnds = ["sam", "mathew"];
var fcall = new Person("alan", frnds);
fcall.saluteFriends();
'sayHello'関数はプロトタイプのプロパティの値です。あなたは単にsayHelloとしてそれを参照することによってそれを呼び出すことはできません。 – Pointy