gpt4 book ai didi

javascript - 扩展 javascript 文字对象

转载 作者:行者123 更新时间:2023-11-30 07:29:11 25 4
gpt4 key购买 nike

为什么 JohnDoe.whoAreYou() 会返回 undefined ?:

<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 = function() {
return this.firstName + " " + this.lastName;
};

关于javascript - 扩展 javascript 文字对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4248222/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com