-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprototypes.js
More file actions
26 lines (19 loc) · 823 Bytes
/
prototypes.js
File metadata and controls
26 lines (19 loc) · 823 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Initialize a constructor function for a new Hero
//The this keyword will refer to the new instance that is created,
//so setting this.name to the name parameter ensures the new object
// will have a name property set.
function Workshop(name, level) {
this.name = name;
this.level = level;
}
let workshop1 = new Workshop('Bjorn', 1);
console.log(workshop1)
//get prototype of workshop1
Object.getPrototypeOf(workshop1);
//You may notice that we've only defined properties and not methods in the constructor.
//It is a common practice in JavaScript to define methods on the prototype for increased efficiency and code readability.
//We can add a method to Hero using prototype. We'll create a welcome() method.
Hero.prototype.welcome = function () {
return `${this.name} says hello.`;
}
workshop1.welcome();