-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsprototype.js
47 lines (39 loc) · 1.53 KB
/
jsprototype.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// In JavaScript, inheritance works a bit differently compared to C++ or Java.
// JavaScript inheritance is more widely known as “prototypical inheritance”.
/*
one object trying to access methods and properties of other object is called prototype
*/
let arr = ['shash','test'];
// arr. when we type arr. we get access to build in functions , methods properties
let obj = {
name : 'shash',
age : 10,
getIntro : function(){
console.log('get access ', this.age , this.na-me)
}
}
// obj. when we type obj. we get access to build in functions , methods properties
function fun(){
}
// fun. when we type fun. we get access to build in functions , methods properties
// these access we get from protype , js engine attaches our obj ,arr , fun with these methods or hidden properties which we get from prototype
// whatever u create in js an object is attached to it which has all those methods.
// EXAMPKE TO CHECK PROTOTYPE
// Array.prototype
// Object.prototype
//prototype chain : prototype of array is an object and prototype of object is an object and this objects prototype is null and this is called prototype chain
//EXAMPLE TO UNDERSTAND PROTOTYPICAL INHERITANCE :
let obj1 = {
name : 'shash',
age : 10,
getIntro : function(){
console.log('get access ', this.age , this.na-me)
}
}
let obj2 = {
name : 'shash'
}
obj2.__proto__ = obj1;
// now if i do
console.log(obj2.age); // 10 since there is no age in obj2
console.log(obj2.name); // shash since name is alredy present in obj2