Skip to content

Commit 0a21694

Browse files
committed
prototypes in js
1 parent 374e32c commit 0a21694

4 files changed

Lines changed: 170 additions & 0 deletions

File tree

10_classes_ans_oop/notes.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# javascript and classes
2+
3+
## OOP
4+
5+
## Object
6+
- collection of properties and methods
7+
- toLowerCase
8+
9+
## why use OOP
10+
11+
## parts of OOP
12+
Object literal
13+
14+
- Constructor function
15+
- Prototypes
16+
- Classes
17+
- Instances (new, this)
18+
19+
20+
## 4 pillars
21+
Abstraction
22+
Encapsulation
23+
Inheritance
24+
Polymorphism

10_classes_ans_oop/object.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
function multipleBy5(num){
2+
3+
return num*5
4+
}
5+
6+
multipleBy5.power = 2
7+
8+
console.log(multipleBy5(5));
9+
console.log(multipleBy5.power);
10+
console.log(multipleBy5.prototype);
11+
12+
function createUser(username, score){
13+
this.username = username
14+
this.score = score
15+
}
16+
17+
createUser.prototype.increment = function(){
18+
this.score++
19+
}
20+
createUser.prototype.printMe = function(){
21+
console.log(`price is ${this.score}`);
22+
}
23+
24+
const chai = new createUser("chai", 25)
25+
const tea = createUser("tea", 250)
26+
27+
chai.printMe()
28+
29+
30+
/*
31+
32+
Here's what happens behind the scenes when the new keyword is used:
33+
34+
A new object is created: The new keyword initiates the creation of a new JavaScript object.
35+
36+
A prototype is linked: The newly created object gets linked to the prototype property of the constructor function. This means that it has access to properties and methods defined on the constructor's prototype.
37+
38+
The constructor is called: The constructor function is called with the specified arguments and this is bound to the newly created object. If no explicit return value is specified from the constructor, JavaScript assumes this, the newly created object, to be the intended return value.
39+
40+
The new object is returned: After the constructor function has been called, if it doesn't return a non-primitive value (object, array, function, etc.), the newly created object is returned.
41+
42+
*/

10_classes_ans_oop/oop.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const user = {
2+
username: "hitesh",
3+
loginCount: 8,
4+
signedIn: true,
5+
6+
getUserDetails: function(){
7+
//console.log("Got user details from database");
8+
// console.log(`Username: ${this.username}`);
9+
console.log(this);
10+
}
11+
12+
}
13+
14+
15+
16+
//console.log(user.username)
17+
//console.log(user.getUserDetails());
18+
// console.log(this);
19+
20+
21+
function User(username, loginCount, isLoggedIn){
22+
this.username = username;
23+
this.loginCount = loginCount;
24+
this.isLoggedIn = isLoggedIn
25+
26+
this.greeting = function(){
27+
console.log(`Welcome ${this.username}`);
28+
29+
}
30+
31+
return this
32+
}
33+
34+
const userOne = new User("hitesh", 12, true)
35+
const userTwo = new User("ChaiAurCode", 11, false)
36+
console.log(userOne.constructor);
37+
//console.log(userTwo);

10_classes_ans_oop/prototype.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// let myName = "hitesh "
2+
// let mychannel = "chai "
3+
4+
// console.log(myName.trueLength);
5+
6+
7+
let myHeros = ["thor", "spiderman"]
8+
9+
10+
let heroPower = {
11+
thor: "hammer",
12+
spiderman: "sling",
13+
14+
getSpiderPower: function(){
15+
console.log(`Spidy power is ${this.spiderman}`);
16+
}
17+
}
18+
19+
Object.prototype.hitesh = function(){
20+
console.log(`hitesh is present in all objects`);
21+
}
22+
23+
Array.prototype.heyHitesh = function(){
24+
console.log(`Hitesh says hello`);
25+
}
26+
27+
// heroPower.hitesh()
28+
// myHeros.hitesh()
29+
// myHeros.heyHitesh()
30+
// heroPower.heyHitesh()
31+
32+
// inheritance
33+
34+
const User = {
35+
name: "chai",
36+
email: "chai@google.com"
37+
}
38+
39+
const Teacher = {
40+
makeVideo: true
41+
}
42+
43+
const TeachingSupport = {
44+
isAvailable: false
45+
}
46+
47+
const TASupport = {
48+
makeAssignment: 'JS assignment',
49+
fullTime: true,
50+
__proto__: TeachingSupport
51+
}
52+
53+
Teacher.__proto__ = User
54+
55+
// modern syntax
56+
Object.setPrototypeOf(TeachingSupport, Teacher)
57+
58+
let anotherUsername = "ChaiAurCode "
59+
60+
String.prototype.trueLength = function(){
61+
console.log(`${this}`);
62+
console.log(`True length is: ${this.trim().length}`);
63+
}
64+
65+
anotherUsername.trueLength()
66+
"hitesh".trueLength()
67+
"iceTea".trueLength()

0 commit comments

Comments
 (0)