Learning Week 8 9 (Beginning May 9th) - emcsquared2/2022-Learning GitHub Wiki
Week 8/9 (Beginning May 9th) Learning Week Cycle 7
GCSE Computer Science
How to use subgroups
Takeaway learning
Value of breaking down programs into subprograms
MIMO mobile app
ES6
Takeaway learning
Const variables can be declared using capitals and snake casing DOG_NAME = "rex"; If we try and assign a new value to a const variable that has been declared, we end up with a TypeError. When using var we can store data in the variable before it has been declared. This is called "hoisting" (not good practice)
zodiacSign = "Aries";
var zodiacSign;
console.log(zodiacSign);
//OUTPUT Aries
let and const prevent hoisting. You would get a reference error
Professor Steve
Introduction to Functions Function Parameters Return Statement ES6 arrow functions Global scope vs local scope Hoisting
Takeaway learning
Revision of concepts already met When a function declaration is called, you can call the function above the function itself because the function declaration is "hoisted" to the top of the page Function expressions are viewed as variables and therefore the variable is hoisted to the top of the page but only as undefined. So the browser knows its there but it will return undefined until the value of the function expression has been read So you have to call the function AFTER a function expression has been written.
Codewars Exercise (Level 8)
- Color Ghost
- Create a class Ghost
- Ghost objects are instantiated without any arguments.
- Ghost objects are given a random color attribute of "white" or "yellow" or "purple" or "red" when instantiated
- ghost = new Ghost();
- ghost.color //=> "white" or "yellow" or "purple" or "red"
My Solution
I got stuck! Community solution...
var Ghost = function() {
this.color = ["white","yellow","purple","red"][Math.floor(Math.random() * 4)];
};
I still don't understand how var Ghost can create a class. I get how the this.color key.value pair is created using the random selection from the arrray but not the class bit. Will have to do some more learning.
MIMO mobile app
Classes
Takeaway learning
class Animal {
contructor(name) {
this.name = name;
}
speak() {
console.log("I am a " + this.name);
}
}
class Dog extends Animal {
constructor(name, trainer) {
super(name);
this.trainer = trainer;
}
speak() {
super.speak();
console.log("My trainer is " + this.trainer);
}
}
var rover = new Dog("Lab", "Terry");
rover.speak();
//OUTPUT
//I am a Lab
//My trainer is Terry
The super keyword allows us to use the properties/methods of the superclass and add any extra properties to the subclass underneath it.
MIMO mobile app
Object Orientated Programming Encapsulation refers to the grouping together of related data and functions in the same object. In Functional programming, code is not encapsulated in an object