Week3-Wednesday
Week3-Wednesday
Track B: JavaScript II
let bellagio =
name: "Bellagio"
location: "Las Vegas"
rooms: 3950
occupiedRooms: 2593
availableRooms: function()
return this.rooms - this.occupiedRooms
}
}
let timberlineLodge =
name: "Timberline Lodge"
location: "Oregon"
rooms: 70
occupiedRooms: 35
availableRooms: function()
return this.rooms - this.occupiedRooms
}
}
Object constructor
• console: used to, e.g., write something in the log with console.lo
• document: the browser’s representation of an HTML page
• window: the browser window, has properties such as window.innerWidth
• Date: Constructor used to create a new date object
let now = new Date();
let day = now.getDay();
Inheritance in JavaScript
• Prototype-based
• Objects inherit methods and properties from other objects
• Inheritance follows a prototype chain
ElectricCar.prototype = Object.create(Car.prototype)
Object.defineProperty(ElectricCar.prototype,
'constructor',
value: ElectricCar
enumerable: false, // so that it does not appear in
'for in' loo
writable: true });
p
Classes in JavaScript
• Introduced with ECMAScript 6 (ES6)
• (mostly) syntactic sugar for prototypical inheritance
• Generally safer to use than doing inheritance “by hand”
class Person
constructor(name, birthYear)
this.name = name
this.birthYear = birthYear
age()
return new Date().getFullYear() - this.birthYear
}
}
}
}