Javascript Hard Parts Oop PDF
Javascript Hard Parts Oop PDF
Name: Phil
Score: 4
Name: Julia
Score: 5
Functionality
+ Ability to increase score
const user1 = {
name: "Phil",
score: 4,
increment: function() {
user1.score++;
}
};
user3.name = "Eva";
user3.score = 9;
user3.increment = function() {
user3.score++;
};
Benefits:
const functionStore = {
increment: function(){this.score++;},
login: function(){console.log("You're loggedin")}
};
const user1 = {
name: "Phil",
score: 4
}
user1.increment // function....
const userFunctionStore = {
increment: function(){this.score++;},
login: function(){console.log("You're loggedin");}
};
Answer these:
csx.codesmith.io
csbin.io/OOP
— No login needed
Solution 3 - Introducing the keyword that automates the
hard work: new
When we call the constructor function with new in front we automate 2 things
But now we need to adjust how we write the body of userCreator - how can we:
function multiplyBy2(num){
return num*2
}
multiplyBy2.stored = 5
multiplyBy2(3) // 6
multiplyBy2.stored // 5
multiplyBy2.prototype // {}
We could use the fact that all functions have a default property
on their object version, ’prototype’, which is itself an object - to
replace our functionStore object
Complete Solution 3
UserCreator.prototype.increment = function(){
this.score++;
};
UserCreator.prototype.login = function(){
console.log("login");
};
user1.increment()
Benefits
— Faster to write
— Still typical practice in professional code
— 99% of developers have no idea how it works and
therefore fail interviews
— We have to upper case first letter of the function so
we know it requires ‘new’ to work!
What if we want to organize our code inside one of our
shared functions - perhaps by defining a new inner
function
UserCreator.prototype.increment = function(){
function add1(){
this.score++;
}
// const add1 = function(){this.score++;}
add1()
};
UserCreator.prototype.login = function(){
console.log("login");
};
user1.increment()
We need to introduce arrow functions - which bind this
lexically
UserCreator.prototype.increment = function(){
const add1 = ()=>{this.score++}
add1()
};
UserCreator.prototype.login = function(){
console.log("login");
};
user1.increment()
Solution 4
class UserCreator {
constructor (name, score){
this.name = name;
this.score = score;
}
increment (){
this.score++;
}
login (){
console.log("login");
}
}
user1.increment();
Benefits:
Problems
const obj = {
num : 3
}
obj.num // 3
obj.hasOwnProperty("num") // ? Where's this method?
function multiplyBy2(num){
return num*2
}
csx.codesmith.io
csbin.io/OOP
— No login needed
Subclassing
We can achieve this in
JavaScript in Solution 2, 3
and 4
Interlude - We have another way of running a function that
allow us to control the assignment of this
const obj = {
num: 3,
increment: function(){this.num++;}
};
const otherObj = {
num: 10
};
this always refers to the object to the left of the dot on which the function (method) is
being called - unless we override that by running the function using .call()
or .apply() which lets us set the value of this inside of the increment function
The Hard Parts Challenge Code