1function Animal (name, energy) {
2 this.name = name
3 this.energy = energy
4}
5
6Animal.prototype.eat = function (amount) {
7 console.log(`${this.name} is eating.`)
8 this.energy += amount
9}
10
11Animal.prototype.sleep = function (length) {
12 console.log(`${this.name} is sleeping.`)
13 this.energy += length
14}
15
16Animal.prototype.play = function (length) {
17 console.log(`${this.name} is playing.`)
18 this.energy -= length
19}
20
21function Dog (name, energy, breed) {
22 Animal.call(this, name, energy)
23
24 this.breed = breed
25}
26
27Dog.prototype = Object.create(Animal.prototype)
28
29Dog.prototype.bark = function () {
30 console.log('Woof Woof!')
31 this.energy -= .1
32}
33
34const charlie = new Dog('Charlie', 10, 'Goldendoodle')
35console.log(charlie.constructor)
1function Person(first, last, age, gender, interests) {
2 this.name = {
3 first,
4 last
5 };
6 this.age = age;
7 this.gender = gender;
8 this.interests = interests;
9};
10
11function Teacher(first, last, age, gender, interests, subject) {
12 Person.call(this, first, last, age, gender, interests);
13
14 this.subject = subject;
15}
1function Person(name) {
2 this.name = name;
3}
4Person.prototype.getName = function() {
5 return this.name;
6}
7
8var person = new Person("John Doe");
9person.getName() //"John Doe"
1function Person(first, last, age, eye) {
2this.firstName = first;
3this.lastName = last;
4this.age = age;
5this.eyeColor = eye;
6}
7
8Person.prototype.nationality = "English";
9
10var myFather = new Person("John", "Doe", 50, "blue");
11console.log("The nationality of my father is " + myFather.nationality)
1/*Prototype is used to add properties/methods to a
2constructor function as in example below */
3
4function ConstructorFunction(name){
5 this.name = name //referencing to current executing object
6}
7ConstructorFunction.prototype.age = 18
8let objectName = new ConstructorFunction("Bob")
9console.log(objectName.age) //18
1// prototypes in javascript, THey create new Methods to our constructor function
2const EmployersInfo = function(name , salary , bonusPercentage){
3 this.name = name ;
4 this.salary = salary;
5 this.bonusPercentage = bonusPercentage;
6}
7
8// prototype for getting the employer credential
9EmployersInfo.prototype.credentialInfo = function(){
10 return `${this.name} Has a base salary of ${this.salary}`;
11}
12
13EmployersInfo.prototype.getBonus = function(){
14 let bonusAmount = (this.salary * (this.bonusPercentage));
15 return `${this.name} earned ${bonusAmount} Bonus`;
16}
17// let's first create the instance of employerInform
18const employerInform = new EmployersInfo('kevin' , 12000 , 12);
19
20// logging employerInform
21console.log(employerInform);
22
23// logging the credentials
24console.log(employerInform.credentialInfo());
25
26// logging the Bonus function
27console.log(employerInform.getBonus());