In JavaScript, an object is basically just a container of properties
How can you get / retrieve a value that's associated with a property name from an object? →
obj.propName
obj[propName]
How do you add a new property and value pair to an object? →
obj.newProp = newVal
or obj[newProp] = newVal
How do you modify an existing property? →
obj.prop = newVal
or obj[prop] = newVal
And finally, how do you remove a property? →
delete
before referencing a property namedelete obj.prop
JavaScript's objects are prototype based rather than class based:
Object.prototype
Object.prototype
)What methods does Object.prototype contain (we definitely know two)? →
toString
hasOwnProperty
How do you retrieve an object's original prototype? →
Object.getPrototypeOf
.prototype
on your object does not actually return your object's prototype!)
const obj = {};
console.log(Object.getPrototypeOf(obj));
console.log(obj.prototype); // <-- don't do this, you'll get undefined
Name 4 ways to create objects. →
Object.create
You can create objects simply by using curly braces!
{}
is an empty object{name1:val1, name2:val2}
… you can create property and value pairs
What do you think the prototype of an object created with an object literal is? →
Object.getPrototypeOf({}) === Object.prototype
How does Object.create work / what does it do? →
You use Object.create(someOtherObject)
to create objects with a specified prototype. The single argument passed in to Object.create
becomes the new object's prototype. …Let's see an example. →
const cat = {cute: 'very'};
const kitten = Object.create(cat);
console.log(kitten.cute); // <-- kitten inherits cute from cat!
What's a constructor? →
A constructor is a regular function called with new
in front of it.
function Cat() { }
const c = new Cat();
How can we set properties on all new objects created by a constructor? →
Use this to refer to a fresh empty object that's returned by the constructor. Add properties to this to set properties on the newly created object. Let's see an example. →
function Cat() { this.cute = 'very'; }
const c = new Cat();
const d = new Cat();
console.log(c.cute, d.cute); // <-- properties on new objects are set!
Using this creates a property name and value for every new object instantiated with the constructor. Each object has its own copy. These properties are considered "own" properties (they haven't been inherited).
function Cat() { this.nicknames = ['katy furry', 'kitty wap']; }
const c = new Cat();
const d = new Cat();
d.nicknames.push('tail-fur swift');
console.log(c.nicknames, d.nicknames); // <-- properties are different!
console.log(c.hasOwnProperty('nicknames')); // <-- yup, bar is an 'own' property
You can also use the constructor's (the actual function object) own prototype (it's really called prototype
!) property to set the properties on the prototype of all instances created by this constructor.
function Cat() { }
const c = new Cat();
Cat.prototype.cute = 'very';
console.log(c.cute);
// ^-- such wow! c gained a new prop even though
// it was instantiated before setting a property
// on Cat's prototype object
Note that since we're setting properties on the prototype object, these properties are not own properties.
function Cat() { }
const c = new Cat();
Cat.prototype.cute = 'very';
console.log(c.cute); // <-- cute exists
console.log(c.hasOwnProperty(c.cute)); // <-- but it's inherited (not an own prop)
You can also just assign an entire object to the prototype property… but this doesn't change all instances like setting individual properties on the constructor's prototype property does.
function Cat() { this.cute = 'very' }
function Kitten() { }
Kitten.prototype = new Cat();
const k = new Kitten(); // <-- instantiated *after* setting prototype!
console.log(k.cute); // <-- cute exists!
A base class:
class Monster {
constructor(name) {
this.name = name;
}
scare(thing) {
console.log(this.name, ' scares ', thing);
}
}
A subclass:
class Werewolf extends Monster {
constructor(name, type) {
super(name);
this.type = type;
}
howl() {
console.log(this.name, this.type, 'werewolf howls at the moon');
}
}
const w = new Werewolf('wally', 'party');
So… classes essentially set up a prototype chain and constructor function. What's the output of this code? →
console.log(typeof Werewolf);
console.log(Object.getPrototypeOf(w) === Werewolf.prototype);
console.log(Object.getPrototypeOf(w).hasOwnProperty('howl'));
console.log(Object.getPrototypeOf(w).hasOwnProperty('scare'));
console.log(Object.getPrototypeOf(Object.getPrototypeOf(w)) === Monster.prototype);
console.log(w instanceof Werewolf);
console.log(w instanceof Monster);
Function
true
true
false
true
true
true