Skip to content

Instantly share code, notes, and snippets.

@cihat
Created November 15, 2020 22:58
Show Gist options
  • Save cihat/e0749fe087f196237f52644d6fabf78a to your computer and use it in GitHub Desktop.
Save cihat/e0749fe087f196237f52644d6fabf78a to your computer and use it in GitHub Desktop.
Object Methods
As mentioned in Chapter 1, you can add and remove properties from
objects at any time. When a property value is actually a function, the
property is considered a method. You can add a method to an object in
the same way that you would add a property. For example, in the following code, the person variable is assigned an object literal with a name property and a method called sayName.
var person = {
name: "Nicholas",
sayName: function() {
console.log(person.name);
}
};
person.sayName(); // outputs "Nicholas"
Note that the syntax for a data property and a method is exactly
the same—an identifier followed by a colon and the value. In the case
of sayName, the value just happens to be a function. You can then call
the method directly from the object as in person.sayName("Nicholas").
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment