mirror of
https://github.com/alrayyes/wiki.git
synced 2024-11-22 19:46:23 +00:00
705 B
705 B
date | id | title |
---|---|---|
2020-11-13 | 32b0031a-0830-4e8c-a39c-3bf6d8791f84 | JavaScript New Keyword |
Description
When you put the keyword new
in front of a function call, the function
is treated as a constructor. An object with
the proper prototype is
automatically created, bound to this
in the function and returned at
the end of the function. This allows you to do OO type stuff.
Syntax
function Rabbit(type) {
this.type = type
}
Rabbit.prototype.speak = function(line) {
console.log(`The ${this.type} rabbit says '${line}'`)
};
let weirdRabbit = new Rabbit("weird")
weirdRabbit.speak("I want carrots!")