-
Notifications
You must be signed in to change notification settings - Fork 0
/
OOJS.js
106 lines (90 loc) · 2.33 KB
/
OOJS.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Object Oriented Java Script
// Generic Object
var obj = {id:101, name:'Ram', city:'Delhi'}; // Object Literal
typeof obj;
'object'
obj instanceof Object;
true
typeof Object; // GOD Function
'function'
// It's not goood to write this like this... writting same thing again n again
var emp = {id:1001, name:'Tim'}
var emp2 = {id:1002, name:'Vim'};
// So we decided to create a function and use object literals..
function createEmp(id, name , salary){
var emp = {}; // Object Literal
emp.id = id;
emp.name = name;
emp.salary = salary;
return emp;
}
var ram = createEmp(1001, 'Ram', 22222);
typeof ram;
'object'
ram instanceof createEmp; // not a specific object
false
ram instanceof Object; // Object is GOD Function
true
// but the issue with this that the object created through this is not the specific object... Object is created through object literal
// Specific Objects
// using Function Constructor
function Person(id, name ,age){
console.log('Person ',this);
this.id = id;
this.name = name;
this.age = age;
console.log('Person with Values ',this);
// this - keyword - current calling object reference
}
var tim = new Person(1001, 'Tim',21);
Person Person {}
Person with Values Person {id: 1001, name: 'Tim', age: 21}
undefined
tim instanceof Person; // Specific Object
true
tim instanceof Object;
true
ram instanceof createEmp; // this is a Generic Object
false
// Prototypes
Employee.prototype;
{constructor: ƒ}
Array.prototype;
[constructor: ƒ, at: ƒ, concat: ƒ, copyWithin: ƒ, fill: ƒ, …]
Array.prototype.forEach;
ƒ forEach() { [native code] }
Array.prototype.map
ƒ map() { [native code] }
Array.prototype.find;
ƒ find() { [native code] }
typeof Array;
'function'
typeof Array.prototype;
'object'
arr;
(3) [10, 20, 30]
arr.__proto__;
[constructor: ƒ, at: ƒ, concat: ƒ, copyWithin: ƒ, fill: ƒ, …]
arr.__proto__ === Array.prototype;
true
Array.prototype.__proto__ === Object.prototype;
true
Employee;
ƒ Employee(id, name, salary){
// this - keyword (Current Calling Object Reference)
this.id = id;
this.name = name;
this.salary = salary;
}
var r = new Employee(1001, "R",9999);
undefined
r;
Employee {id: 1001, name: 'R', salary: 9999}
class Student{}
undefined
var s = new Student();
undefined
typeof s;
'object'
s instanceof Student;
true