JavaScript方法不是在工作的jsfiddle

问题描述 投票:1回答:1

很简单的问题。我刚开始学习JavaScript前几天,和我使用的jsfiddle来运行和测试我的代码。我有这个简单的方法,我想在的jsfiddle运行,但不知道为什么它没有运行。任何帮助,将不胜感激。

var person = {
    firstName: "John",
    lastName: "Doe",
    fullName: function () {
        return this.firstName + " " + this.lastName;
    }
}
person.fullName();
javascript jsfiddle
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.