Java脚本方法在JSFiddle中不起作用

时间:2019-02-05 21:06:52

标签: javascript jsfiddle

非常简单的问题。几天前我才刚刚开始学习JavaScript,并且我正在使用JSFiddle运行和测试我的代码。我有尝试在JSFiddle中运行的这种简单方法,但是不确定为什么它没有运行。任何帮助,将不胜感激。

var person = {
    firstName: "John",
    lastName: "Doe",
    fullName: function () {
        return this.firstName + " " + this.lastName;
    }
}
person.fullName();

1 个答案:

答案 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