0
爲什麼要撥打alert()
彈出undefined
?我猜測,Javascript沒有使用我認爲是的this
。我怎樣才能修復代碼,同時仍然使它看起來像非原型繼承代碼?爲什麼會員未定義?
<!DOCTYPE html>
<html>
<head><title>Help</title>
<script type="text/javascript">
var Foo = function (myName) {
this.myName = myName;
}
Foo.prototype.sayName = function() {
alert(this.myName);
}
var foo = new Foo("I am foo");
var bar = {
ask: function (fn) {
fn();
}
};
function doIntroductions() {
bar.ask(foo.sayName);
}
</script>
</head>
<body onload="doIntroductions();">
</body>
</html>