0

If I have the following code:

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

What is it called? Is it just a JSON object, even though it has a function binding? In the example I gave, could I call fullName a method?

kuwze
  • 411
  • 4
  • 13

1 Answers1

1

You can find the difference in JSON & Object literal here.

  • person is an object literal.

  • Properties (firstName, lastName, id) is like a noun which refers the person details.

  • Method (fullName) is like a verb that describes an action.

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

console.log("FirstName :", person.firstName);
console.log("Full Name :", person.fullName());

Here, person is an object. It has a property person.firstName, person.lastName, person.id and method person.fullName() that return full name of the person which include the person firstName & lastName property.

Debug Diva
  • 26,058
  • 13
  • 70
  • 123