INFO 253A: Front End Web Architecture
Kay Ashaolu
class Planet {
constructor (mass, moons) {
this.mass = mass;
this.moons = moons || 0;
}
reportMoons () {
console.log(`I have ${this.moons} moons.`)
}
}
var _createClass = function () { function defineProperties(target, props) {...
defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
var Planet = function () {
function Planet(mass, moons) {
_classCallCheck(this, Planet);
this.mass = mass;
this.moons = moons || 0;
}
_createClass(Planet, [{
key: 'reportMoons',
value: function reportMoons() {
console.log('I have ' + this.moons + ' moons.');
}
}]);
return Planet;
}();
A lot of improvements to language focuses on changing syntax to make it easier to accomplish a certain goal
Let's talk about some of those features in ES6
let a = 50;
let b = 100;
if (true) {
let a = 60;
var c = 10;
console.log(a/c); // 6
console.log(b/c); // 10
}
console.log(c); // 10
console.log(a); // 50
const b = "Constant variable";
b = "Assigning new value"; // shows error.
const LANGUAGES = ['Js', 'Ruby', 'Python', 'Go'];
LANGUAGES = "Javascript"; // shows error.
LANGUAGES.push('Java'); // Works fine.
console.log(LANGUAGES); // ['Js', 'Ruby', 'Python', 'Go', 'Java']
function oldOne(name) {
console.log("Hello " + name);
}
oldOne("Kay");
// New Syntax
let newOne = (name) => {
console.log("Hello " + name);
}
newOne("Kay");
let Func = (a, b = 10) => {
return a + b;
}
console.log(Func(20)); // 20 + 10 = 30
console.log(Func(20, 50)); // 20 + 50 = 70
let NotWorkingFunction = (a = 10, b) => {
return a + b;
}
console.log(NotWorkingFunction(20)); // NAN. Not gonna work.
let arr = [2,3,4,1];
for (let value of arr) {
console.log(value);
}
let SumElements = (...arr) => {
console.log(arr); // [10, 20, 40, 60, 90]
let sum = 0;
for (let element of arr) {
sum += element;
}
console.log(sum);
}
SumElements(10, 20, 40, 60, 90);
SumElements(10, 20, 90);
let name = "Jon Snow";
let msg = `My name is ${name}`;
console.log(msg);
let person = {firstName: "Jon", lastName: "Snow", age: 23}
const {firstName, age} = person
console.log(firstName);
console.log(age);
let arr = [1,2,3,4,5,6]
let [a,b,,d,e] = arr
console.log(a);
console.log(b);
console.log(d);
console.log(e);