非常简单的问题。几天前我刚开始学习JavaScript,我正在使用JSFiddle来运行和测试我的代码。我有一个简单的方法,我正在尝试在JSFiddle中运行,但不确定为什么它没有运行。任何帮助将不胜感激。
var person = {
firstName: "John",
lastName: "Doe",
fullName: function () {
return this.firstName + " " + this.lastName;
}
}
person.fullName();
你提出的代码应该运行得很好,但是在下面的评论中看看你的函数fullName
实际在做什么:
var person = {
firstName: "John",
lastName: "Doe",
fullName: function() {
// notice you are returning a string value
return this.firstName + " " + this.lastName;
}
};
// store the value you are returning in a variable
var personFullName = person.fullName();
// print out full name to the console
console.log(personFullName);
查看此资源以获取return
关键字和快乐编码!
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return