DEV Community

Kaziu
Kaziu

Posted on ā€¢ Edited on

1 1

šŸ¤” For what prototype??

what is prototype?

simply put, special property in object

For example

function Worker(name, age) {
  this.name = name
  this.age = age
}

// ā­ prototype !!
Worker.prototype.msg = function() {
  console.log('I am ' + this.name)
}

const maria = new Worker('Maria', 20)
const kaziu = new Worker('Kaziu', 27)
const jhon = new Worker('Jhon', 37)

maria.msg() // I am Maria
kaziu.msg() // I am Kaziu
jhon.msg() // I am Jhon 
Enter fullscreen mode Exit fullscreen mode

ā–¼ But we could write like that

function Worker(name, age) {
  this.name = name
  this.age = age
  // ā­ like this ####################
  this.msg = function() {
    console.log('I am ' + this.name)
  }
  // ###########################
}

const maria = new Worker('Maria', 20)
const kaziu = new Worker('Kaziu', 27)
const jhon = new Worker('Jhon', 37)

maria.msg() // I am Maria
kaziu.msg() // I am Kaziu
jhon.msg() // I am Jhon
Enter fullscreen mode Exit fullscreen mode

šŸ¤” So for what we use prototype ??
šŸ˜Ž One of the reason is memory management

Image description

If you don't use prototype, each instance would create new function on new memory preference when each time instance is created

Image description

Otherwise function doesn't create each time when instance is created as you see in image.
Each instance just look for preference of prototype (= instances look for memory place of prototype function)

ref. from this course

Image of Datadog

The Future of AI, LLMs, and Observability on Google Cloud

Datadog sat down with Googleā€™s Director of AI to discuss the current and future states of AI, ML, and LLMs on Google Cloud. Discover 7 key insights for technical leaders, covering everything from upskilling teams to observability best practices

Learn More

Top comments (0)

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

šŸ‘‹ Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay