You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// 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
+
functionmultiply5(num){
11
+
returnnum*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
+
functioncreateUser(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
+
constchai=createUser("chai",25)
36
+
// console.log(chai);
37
+
consttea=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
+
constnewchai=newcreateUser("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.
0 commit comments