forked from loaialaddien/javascriptNotes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjects.js
82 lines (62 loc) · 2.14 KB
/
objects.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
///objects
/*
Object Data Properties Have Attributes
Each data property (object property that store data) has not only the name-value pair,
but also 3 attributes (the three attributes are set to true by default):
—
Configurable Attribute: Specifies whether the property can be deleted or changed.
— Enumerable: Specifies whether the property can be returned in a for/in loop.
— Writable: Specifies whether the property can be changed.
*/
// This is an empty object initialized using the object literal notation
var myBooks = {};
// This is an object with 4 items, again using object literal
var mango = {
color: "yellow",
shape: "round",
sweetness: 8,
howSweetAmI: function() {
console.log("Hmm Hmm Good");
}
}
//object constructor
var mango = new Object();
mango.color = "yellow";
mango.shape = "round";
mango.sweetness = 8;
mango.howSweetAmI = function() {
console.log("Hmm Hmm Good");
}
//Constructor Pattern for Creating Objects
function Fruit(theColor, theSweetness, theFruitName, theNativeToLand) {
this.color = theColor;
this.sweetness = theSweetness;
this.fruitName = theFruitName;
this.nativeToLand = theNativeToLand;
this.showName = function() {
console.log("This is a " + this.fruitName);
}
this.nativeTo = function() {
this.nativeToLand.forEach(function(eachCountry) {
console.log("Grown in:" + eachCountry);
});
}
}
//Prototype Pattern for Creating Objects
function Fruit() {
}
Fruit.prototype.color = "Yellow";
Fruit.prototype.sweetness = 7;
Fruit.prototype.fruitName = "Generic Fruit";
Fruit.prototype.nativeToLand = "USA";
Fruit.prototype.showName = function() {
console.log("This is a " + this.fruitName);
}
Fruit.prototype.nativeTo = function() {
console.log("Grown in:" + this.nativeToLand);
}
//cloning objects
var obj = { a: 1, b: 2 }
var objclone = Object.assign({}, obj);
// Object.clone() will just do a shallow copy, not a deep copy.
// This means that nested objects aren’t copied. They still refer to the same nested objects as the original: