forked from LEARNAcademy/JS-foundations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjects-and-json.js
72 lines (62 loc) · 1.86 KB
/
objects-and-json.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
// plain javascript object
// - is a data type
// - information is stored in "attributes" - which are made up of a key - value pairs
// - attributes can store any type of value: strings, numbers, functions, arrays etc...
// - you will also hear them referred to object literals
// ----------------------------- EXAMPLE 1 ----------------------------------------------------
var car = {
doors: 4,
model: "Nissan",
drive: function(name){
console.log("go fast, " + name + this.doors)
}
}
car.drive("bob")
console.log(car)
// ----------------------------- EXAMPLE 2 ----------------------------------------------------
// var user = {
// firstName: "Sharon",
// lastName: "Farmer",
// fullName: function(){
// return this.firstName + " " + this.lastName
// },
// status: "active",
// toggleStatus: function(){
// if(this.status === "active"){
// this.status = "inactive"
// } else {
// this.status = "active"
// }
// }
// }
//
// console.log(user.fullName())
//
// console.log(user.status)
//
// user.toggleStatus()
//
// console.log(user.status)
// ============================================================================================
// JSON
// - is a lightweight data transfer syntax in string form
// - shares a common structure with Javascript objects
// - information is stored in "attributes" - which are made up of a key - value pairs
// - attributes can store strings, numbers, other objects, and arrays - BUT NOT functions
// - relies on a nested structure to provide quick access to data
// ----------------------------- EXAMPLE 1 ----------------------------------------------------
// var car = {
// "doors": 4,
// "model": "Nissan",
// "year": 1999,
// "gear": ["roofrack", "stereo"],
// "previousOwners": {
// "name": "bob",
// "years": "1999-2008",
// "maintenance": [
// {"brakes": "2000"}
// ]
// }
// }
//
// console.log(car)