-
Notifications
You must be signed in to change notification settings - Fork 634
/
Copy pathobjects.html
115 lines (92 loc) · 3.1 KB
/
objects.html
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>
var Person = {}; //Object creation
var Person = { name: 'Pradeep', address: 'Bangalore' }; //Object key value pair
console.log('Access Object using key: ' + Person.name); //Access Object value using key
var Person = [
{ 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
var CarClass = new Object();
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
var d = new Object();
var d = new Object(null);
var d = new Object(undefined);
console.log('Using Object() constructor: ' + typeof d);
// ------------------
// Object.create()
// ------------------
var a = Object.create(null);
console.log('Using Object.create(): ' + typeof a);
// ---------------------
// bracket's syntactig
// ---------------------
var b = {};
console.log('Using Bracket: ' + typeof b);
// -----------------------
// function constructor
// -----------------------
var Obj = function(name) {
this.name = name;
};
var c = new Obj('Pradeep');
console.log('Using function constructor: ' + c.name);
// --------------------
// ES6 class syntax
// --------------------
class myObject {
constructor(name) {
this.name = name;
}
}
var e = new myObject('Pradeep');
console.log('ES6: ' + e.name);
// --------------------
// Singleton pattern
// --------------------
var sp = new function() {
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
**/
var oldObject = [{object:'a'}, {object:'b'}];
var newObject = JSON.parse(JSON.stringify(oldObject));
var newObject = Object.assign({}, oldObject);
console.log('Cloned Object: '+ newObject[0].object); // a
</script>
<body>
Objects in JavaScript
</body>
</html>