很简单的问题。我刚开始学习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