-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjects.js
More file actions
92 lines (75 loc) · 2.43 KB
/
Copy pathobjects.js
File metadata and controls
92 lines (75 loc) · 2.43 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
//An object is a value type that contains key-value pairs inside
//curly braces. The keys are also known as properties.
//Anything in JavaScript that isn't a primitive type is an object
//Primitive types are strings, numbers, booleans, undefined, null
//when variables are used in objects they are called properties
//when functions are used in objects, they are called methods
//object literal. AN object literal simply mean you create a variable
//and assign it an object right then
var truck = {
// properties
make: 'Dodge',
wheels: 4
};
// Bracket notation
console.log(`I have a ${truck['make']} that has ${truck['wheels']} wheels`);
// Dot notation
console.log(`I have a ${truck.make} that has ${truck.wheels} wheeels`);
//Object literal
var employee = {
// properties
firstName: 'Joe',
lastName: 'Blow',
// method
fullName: function() {
return `${this.firstName} ${this.lastName}`;
}
};
// Call a method
console.log(`The new employee's name is ${employee.fullName()}`);
// Object constructor function
function Vehicle(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
this.getFullDescription = function() {
return `${this.year} ${this.make} ${this.model}`;
};
}
// Create new instances with the new keyword
let myTruck = new Vehicle('Dodge', 'Ram 1500', '2011');
let myWifesCar = new Vehicle('Dodge', 'Charger R/T', '2015');
let myDaughtersCar = new Vehicle('Nissan', 'Rogue', '2016');
console.log(myTruck.year, myTruck.make, myTruck.model);
console.log(myWifesCar.getFullDescription());
//instead of using this like in the above function
function createVehicle(make, model, year) {
return {
make: make,
model: model,
year: year,
getFullDescription: function() {
return `${year} ${make} ${model}`;
}
};
}
let myTruck2 = createVehicle('Dodge', 'Ram 1500', '2011');
let myWifesCar2 = createVehicle('DOdge', 'Charger R/T', '2015');
let myDaughtersCar2 = createVehicle('Nissan', 'Rogue', '2016');
console.log(myTruck2.getFullDescription());
console.log(myWifesCar2.getFullDescription());
console.log(myDaughtersCar2.getFullDescription());
//ES6 Enhanced Object literal
function createVehicle(make, model, year) {
return {
make,
model,
year,
getFullDescription() {
return `${year} ${make} ${model}`;
}
};
}
console.log(myTruck2.getFullDescription());
console.log(myWifesCar2.getFullDescription());
console.log(myDaughtersCar2.getFullDescription());