Skip to content

Commit 3ef3ad1

Browse files
committed
prototypes
1 parent c140386 commit 3ef3ad1

2 files changed

Lines changed: 142 additions & 0 deletions

File tree

15_OOPs/object.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Behaviour of the Javascript..
2+
// JS has Protypal Behaviour it cheking from child if not found goes to parent then grand-parent untill he got NULL value
3+
4+
// May be reason behind we cannot use this in the arrow function
5+
6+
// Protype is the only whi give you access to new keyword, classes, this keyword or protypial inheritance or inheritance it also due to prototype
7+
8+
// In JS end of the day everything is object and this object indicate the null..
9+
10+
function multiply5(num){
11+
return num*5;
12+
}
13+
14+
multiply5.power = 2;
15+
16+
console.log(multiply5(5)) // 25
17+
console.log(multiply5.power) // 2
18+
console.log(multiply5.prototype) // {}
19+
20+
// In JS, function is fucntion and object to we can use it like a object too in our code
21+
// In Prototype all his own properties + context of this keyword also stored in the prototype
22+
23+
function createUser(username, score){
24+
this.username = username;
25+
this.score = score;
26+
}
27+
28+
createUser.prototype.increment = function(){
29+
this.score++; // this means jis (jis ne bhi bulaya hai usska kam kar do)
30+
}
31+
createUser.prototype.printme = function(){
32+
console.log(`price is ${this.score}`);
33+
}
34+
35+
const chai = createUser("chai", 25)
36+
// console.log(chai);
37+
const tea = createUser("tea", 250)
38+
39+
// myArray.prototype.map() -> we are not declare like this
40+
// myArray.map() -> correct
41+
42+
// chai.printme() -> It gives you error and here the new key word is important..
43+
// we add the value to the variable is fine but it shows you undefine cause we never define that additional properties which createUser hold variable we just transfer the values in it..
44+
45+
const newchai = new createUser("chai", 30);
46+
47+
newchai.printme()
48+
49+
// NOTES =>
50+
51+
/*
52+
Here's what happens behind the scenes when the new keyword is used:
53+
54+
A new object is created: The new keyword initiates the creation of a new JavaScript object.
55+
56+
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.
57+
58+
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.
59+
60+
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.
61+
62+
*/

15_OOPs/prototype.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// GOAL ->
2+
// let myName = "hitesh ";
3+
// let mychannel = "chai ";
4+
5+
// console.log(myName.truelength) // aisa method
6+
7+
8+
// Factory function is nothing but the function that is used to create the object called a factory function..
9+
let myHeros = ["Thor", "Spider-man"];
10+
11+
let heroPower = {
12+
thor: "Hammer",
13+
spiderman: "sling",
14+
15+
getSpiderPower: function(){
16+
console.log(`Spidy power is ${this.spiderman}`);
17+
}
18+
}
19+
20+
// Factory Fucntion => .create
21+
Object.prototype.hitesh = function(){
22+
console.log(`hitesh is present in all Object!`)
23+
}
24+
25+
heroPower.hitesh(); // now hitesh is presen in the prototype
26+
27+
// now check array also contain or not the hitesh
28+
myHeros.hitesh()
29+
// Yes, Array also access the this hitesh prototype
30+
31+
// But here you inject or add property to direct object but, what happen when you add this to the array
32+
Array.prototype.heyHitesh() = function(){
33+
console.log(`Hitesh Says Hello!`);
34+
}
35+
// techniqualy myHero which is an array have access to heyHisteh but the
36+
// heroPower which is object does not have an access of heyHitesh
37+
myHeros.heyHitesh() // -> Arrays has power, Yes
38+
// heroPower.heyHitesh() -> Correct, it doesn't has access to it.. gives you error..
39+
40+
41+
// inheritance
42+
43+
const User = {
44+
name: 'chai',
45+
email: 'chai@google.com'
46+
}
47+
const Teacher = {
48+
makeVideo: true,
49+
}
50+
51+
const TeachingSupport = {
52+
isAvailable: false
53+
}
54+
55+
const TASupport = {
56+
makeAssignment: 'JS Assignment',
57+
fullTime: true,
58+
__proto__: TeachingSupport,
59+
}
60+
61+
Teacher.__proto__ = User // This is Prototypial Inheritance..
62+
// Kisi Aur ki properties ko ham kaise access kar sakte hai isse hai
63+
// prototypial inheritance bol te hai...
64+
65+
// Modern Approach
66+
Object.setPrototypeOf(TeachingSupport, Teacher);
67+
68+
// Now GOAL =>
69+
let myName = "ChaiAurCode ";
70+
71+
String.prototype.truelength = function(){
72+
console.log(`${this}`)
73+
// console.log(`${this.name}`) iss ka access nahi hai hamare pass old
74+
// code mein hota tha ab nahi hota
75+
console.log(`True Length is: ${this.trim().length}`)
76+
}
77+
myName.truelength()
78+
79+
"Hitesh ".truelength()
80+
"iceTea".truelength()

0 commit comments

Comments
 (0)