-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobj.js
147 lines (117 loc) · 3.62 KB
/
obj.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// creating object using method
function createUser(firstName, lastName, email, age, address){
const user = {};
user.firstName = firstName;
user.lastName = lastName;
user.email = email;
user.age = age;
user.address = address;
user.about = function(){
return `${this.firstName} is ${this.age} years old.`;
};
user.is18 = function(){
return this.age >= 18;
}
return user;
}
// insted of use with you can use constructor
function User(firstName, lastName, email, age, address){
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.age = age;
this.address = address;
this.about = function(){
return `${this.firstName} is ${this.age} years old.`;
};
this.is18 = function(){
return this.age >= 18;
}
}
// const u1 = createUser("harshad","parmar","parmar@mail.com",23,"gujarat");
// console.log(u1);
// console.log(u1.about());
// console.log(u1.is18());
const u1 = new User("harshad","parmar","parmar@mail.com",23,"gujarat");
console.log(u1);
console.log(u1.about());
console.log(u1.is18());
const band = {
bandName : "led zepplin",
famousSong: "stariway to heaven",
year:2001,
viralSong:"yararanna..",
walk(){
console.log("this is the walk guys");
},
shllow : function(){
console.log("this is the variable")
}
};
console.log(band.walk());
console.log(band.shllow());
// for (const it of band) {
// console.log(it);
// }
console.log("-----------------------------------------------------------------------------------------------------")
const {bandName ,famousSong,...restcon } = band;
// you can implemented it below type
// to use with value1 and value2 to assign the value
// const {bandName : value1,famousSong : value2} = band;
// console.log(value1,value2);
// restone assign the rest of variable as object form
console.log(bandName,famousSong,restcon)
//nested destructuring
const users = [
{userId: 1,firstName: 'harshit', gender: 'male'},
{userId: 2,firstName: 'mohit', gender: 'male'},
{userId: 3,firstName: 'nitish', gender: 'male'},
]
// to add on the property into the object
for(const u of users){
u.val = 89;
console.log(u,typeof u);
}
// const [user1,...resUser] = users;
// console.log(user1,resUser)
// const obj = {
// pro:"property",
// arr : [5,6,4,5,6],
// fun : function(first,second){
// console.log(this.pro,this.arr,first,second)
// }
// }
// const obj2 = {
// pro : "property2",
// arr:[23,78,45]
// }
// // to use with property of any declared object
// obj.fun.call(obj2,"obj2","obj2-last")
// // same as call but here parameter as array form
// obj.fun.apply(obj2, ["first-name","last-name"]);
// // bind method is similar but its return the function
// const method = obj.fun.bind(obj2,"obj2","obj2-last")
// method();
console.log("-----------------------------------------------------------------------------------------------------")
// prototype
// function constructor
function Person(name, job, yearOfBirth){
this.name= name;
this.job= job;
this.yearOfBirth= yearOfBirth;
}
// this will show Person's prototype property.
console.log(Person.prototype);
// this property
const person = {
name : 'renish',
walk(){
// console.log("should be walk due to overweight");
console.log(this);
}
}
// person.walk()
const walk = person.walk //assign the refernce is the walk
// walk() //return the global object which is the window
// but if as strict mode on (in ES6 by difualt is on) than goes error
console.log(walk,typeof walk)