forked from learning-zone/javascript-basics
- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathobjects.html
More file actions
Latest commit
115 lines (92 loc) · 3.1 KB
/
Copy pathobjects.html
File metadata and controls
115 lines (92 loc) · 3.1 KB
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
<!DOCTYPE html>
<html>
<head>
<title>Objects in JS</title>
</head>
<script>
varPerson={};//Object creation
varPerson={name: 'Pradeep',address: 'Bangalore'};//Object key value pair
console.log('Access Object using key: '+Person.name);//Access Object value using key
varPerson=[
{name: 'ABC',address: 'INDIA'},//Array of Objects
{name: 'XYZ',address: 'USA'},
{name: 'PQR',address: 'UK'}
];
console.log('Name: '+Person[2].name+' and Adress: '+Person[2].address);//Access Object value using key
varCarClass=newObject();
CarClass.company='Ford';
CarClass.model='Mustang';
CarClass.year=1969;
console.log('Car Model: '+CarClass.model);
CarClass['company']='Jaguar';
CarClass['model']='Jaguar Land Rover';
CarClass['year']=1933;
console.log('Car Model: '+CarClass.model);
/**
Multiple ways of creating object in javascript
**/
// ----------------------
// Object() constructor
// ----------------------
// Store an empty Object object in d
vard=newObject();
vard=newObject(null);
vard=newObject(undefined);
console.log('Using Object() constructor: '+typeofd);
// ------------------
// Object.create()
// ------------------
vara=Object.create(null);
console.log('Using Object.create(): '+typeofa);
// ---------------------
// bracket's syntactig
// ---------------------
varb={};
console.log('Using Bracket: '+typeofb);
// -----------------------
// function constructor
// -----------------------
varObj=function(name){
this.name=name;
};
varc=newObj('Pradeep');
console.log('Using function constructor: '+c.name);
// --------------------
// ES6 class syntax
// --------------------
classmyObject{
constructor(name){
this.name=name;
}
}
vare=newmyObject('Pradeep');
console.log('ES6: '+e.name);
// --------------------
// Singleton pattern
// --------------------
varsp=newfunction(){
this.name='Pradeep';
}();
console.log('Using Singleton Pattern: '+sp.name);
console.log('Object length: '+Object.length);// 1
/**
Object Clone in JS
Deep copy by performance: ( best to worst )
1) Reassignment "=" (string arrays, number arrays - only)
2) Slice (string arrays, number arrays - only)
3) Concatenation (string arrays, number arrays - only)
4) Custom function: for-loop or recursive copy
5) jQuery's $.extend
6) JSON.parse (string arrays, number arrays, object arrays - only)
7) Underscore.js's _.clone (string arrays, number arrays - only)
8) Lo-Dash's _.cloneDeep
**/
varoldObject=[{object:'a'},{object:'b'}];
varnewObject=JSON.parse(JSON.stringify(oldObject));
varnewObject=Object.assign({},oldObject);
console.log('Cloned Object: '+newObject[0].object);// a
</script>
<body>
Objects in JavaScript
</body>
</html>