简体   繁体   中英

Java Script Methods are not working in JSFiddle

Very simple question. I just began learning JavaScript a few days ago, and I am using JSFiddle to run and test my code. I have this simple method I am trying to run in JSFiddle, but not sure why it is not running. Any help would be appreciated.

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

The code you put up should run just fine, but take a look at what your function fullName is actually doing in the comments below:

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);

Check out this resource for the return keyword and happy coding!

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM