-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
- *Explain in your own words how you understand prototypical inheritance works in JavaScript.* | ||
- Mainly, prototypical inheritance is technique that used for objecs, so ,objects can inherit properties and methods from other objects. Object's prototype when each object has an internal "link" to another object. When we want to access a property on an object, js first checks if that property exists on the object itself. If it doesn't, it follows the prototype chain. This process continuing until it finds the property or until it reaches the end of the prototype chain. If prototype can be found then js returns its value. | ||
|
||
- *Why is it necessary to call super() in the constructor of a child class?* | ||
- If you check my homework, in Programmer class i used super(). Because Programmer class (child) extends from Employee class (parent). And properties of Employee are same with properties of Programmer. So for not repeating same code we use super() and it establish inheritance relationship, Initialize the parent class. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Document</title> | ||
</head> | ||
<body> | ||
<script src="./script.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
class Employee{ | ||
constructor(name,age,salary) { | ||
this.name = name; | ||
this.age = age; | ||
this.salary = salary; | ||
} | ||
getName(){ | ||
return this.name | ||
} | ||
getAge(){ | ||
return this.age | ||
} | ||
getSalary(){ | ||
return this.salary | ||
} | ||
setName(name) { | ||
this.name = name; | ||
} | ||
|
||
setAge(age) { | ||
this.age = age; | ||
} | ||
|
||
setSalary(salary) { | ||
this.salary = salary; | ||
} | ||
|
||
} | ||
let a = new Employee ("Tunzala","20","1000") | ||
console.log(`${a.getName()} has ${a.getAge()} and gets ${a.getSalary()} from company`) | ||
|
||
class Programmer extends Employee{ | ||
constructor(name,age,salary,lang){ | ||
super(name, age, salary) | ||
this.lang=lang; | ||
} | ||
getLanguage(){ | ||
return this.lang | ||
} | ||
setLanguage(lang) { | ||
this.lang = lang; | ||
} | ||
getMultipliedSalary(){ | ||
return super.getSalary()*3 | ||
} | ||
} | ||
|
||
let b = new Programmer ("Tunzala",20,1000,"JavaScript") | ||
console.log(`${b.getName()} learns ${b.getLanguage()} and earns ${b.getMultipliedSalary()} `) | ||
let i = new Programmer ("Alex", 34, 2000, "C++, Python" ) | ||
console.log(i) | ||
console.log(i.getMultipliedSalary()) |