-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjects.js
67 lines (50 loc) · 2.42 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
/* Objects are similar to arrays, except that instead of using indexes to access and modify their data,
you access the data in objects through what are called properties. However, you can also use numbers as properties.
You can even omit the quotes for single-word string properties. However, if your object has any non-string properties,
JavaScript will automatically typecast them as strings. */
var cat = {
name: "Whiskers",
legs: 4,
tails: 1,
enemies: ["Water", "Dogs"]
};
/* There are two ways to access the properties of an object: dot notation (.) and bracket notation ([]), similar to an array.
Dot notation is what you use when you know the name of the property you're trying to access ahead of time. */
var myObj = {
prop1: "val1",
prop2: "val2"
};
var prop1val = myObj.prop1; //* myObj["prop1"];
var prop2val = myObj.prop2; //* myObj["prop2"];
// for property names with a space you can only use square brackets, e.g. myObj["the first property"];
/* Another use of bracket notation on objects is to access a property which is stored as the value of a variable.
This can be very useful for iterating through an object's properties or when accessing a lookup table. */
var dogs = {
Fido: "Mutt",
Hunter: "Doberman",
Snoopie: "Beagle"
};
var myDog = "Hunter";
var myBreed = dogs[myDog];
console.log(myBreed); //* returns "Doberman"
//* Changing OR adding new properties of an object
dogs.Fido = "Golden Retriever"; //* OR
dogs["Fido"] = "Yorkshire Terrier";
dogs["Yeontan"] = "Pomeranian";
// Deleting a property
delete dogs.Hunter;
// .hasOwnProperty(propname) for checking if property exists within an object
function checkObj(obj, checkProp) {
if (obj.hasOwnProperty(checkProp)) {
return (obj[checkProp]);
}
return "Not Found";
}
console.log(checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "house")); // Not Found
console.log(checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "pet")); // kitten
// ES6
// Use let instead of var to declare a variable that cannot be changed
// If let is used inside a block, statement or expression, it only applies to that part
// const works in a similar way, but it's a read-only variable; typially should be declared with all caps
// Objects declared with let or const are still mutable, but you cannot use the variable identifier to point to a different array using the assignment operator
// Concise Object Literal Declarations Using Object Property Shorthand