profileRyan KesPGP keyI build stuffEmailGithubTwitterLast.fmMastodonMatrix

JavaScript Object Prototype Methods

Description

JavaScript objects have methods.

Syntax

Basic syntax

Objects can also have methods

let robot = {}
robot.speak = function (name) {
  console.log(`${name} is alive!`)
}

robot.speak("Johnny 5")

Properties

Of course methods can use object properties and functions can be passed as properties

let robot = { name: "Johnny 5", speak }
function speak(state) {
  console.log(`${this.name} is ${state}!`)
}

robot.speak("alive")

This

this parameter can also be passed explicitly using a functions call method, if that's socially acceptable in your culture:

let robot = { name: "Johnny 5" }

function speak(state) {
  console.log(`${this.name} is ${state}!`)
}

speak.call(robot, "alive")

Related