爲什麼JohnDoe.whoAreYou()並返回undefined:擴展JavaScript的文本對象
<html>
<head>
</head>
<body>
<script>
var JohnDoe = {
//public property
firstName: "John",
lastName: "Doe",
//public method
whoAreYou: function() {
alert("I am literal object and my name is " + this.toString());
},
whatIsYourAge: function() {
alert("My age is " + this.Age);
}
};
</script>
<script>
JohnDoe.Age = 10;
JohnDoe.toString = function() {this.firstName + " " + this.lastName};
JohnDoe.whoAreYou();
JohnDoe.whatIsYourAge();
</script>
</body>
</html>
首先,你要這樣:JohnDoe.toString =函數(){返回this.firstName + 「」 + this.lastName} – 2010-11-22 17:28:38