非常简单的问题。几天前我才刚刚开始学习JavaScript,并且我正在使用JSFiddle运行和测试我的代码。我有尝试在JSFiddle中运行的这种简单方法,但是不确定为什么它没有运行。任何帮助,将不胜感激。
var person = {
firstName: "John",
lastName: "Doe",
fullName: function () {
return this.firstName + " " + this.lastName;
}
}
person.fullName();
答案 0 :(得分:1)
您放置的代码应该可以正常运行,但是请在下面的注释中查看您的函数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