Describes features, syntax, and behavior of all implementations
Think of it as a ruleset or blueprint
β Implementation:
JavaScript, the implementation of the ECMAScript standard
Implementations track standards
π We'll implement the new features from versions 6 and 7 of ECMAScript using JavaScript
Why use ES6?
Sure, there are handy new methods, syntax, and easier-to-read code, but...
Top JavaScript libraries will be written in ES6. If you aren't familiar with ES6, you'll be at a disadvantage when trying to read documentation and build new features.
Backwards compatible
Integrates nicely with modern build tools
π Above all, it's the new standard. This means that, in the near future, ES6 will be standard in each browser.
Today's Topics
Transpiling
Scopes & Variables
String Helpers
Transpiling
Transpiling = Transformation + Compiling
Before: ES6
const getMessage = () => 'Hello World';
After: ES5
Waiting...
Benefits to Transpiling
Browser support
No need to wait for browsers/engines to catch up with the ECMAScript specification
Focus on writing clean, easy-to-read code — compile it to a cross-browser-compatible format
JavaScript will continue to evolve constantly; without transpilation, you'll miss out on innovations that make JavaScript more effective, efficient, and robust
Transpiling Setup
You can set this up on your machine using Node.js. For now, CodePen can use Babel to transpile ES6 for us.
Scopes & Variables
Scopes π¬
Block Scopes (a.k.a. lexical scopes):
{
// anything within { } that isn't a function
}
if (isPhiladelphia) {
var alwaysSunny = true;
}
Examples:{ }, if () { }, for() {}, while() { }, etc.
Purpose: Declare variables as close as possible, as local as possible, to where they will be used.
Function Scopes:
function func() {
// function required
}
Purpose: Code wrapped in a function effectively "hides" any enclosed variable or function declarations from the outside scope.
Only one potential case: If you have code that should be available from the global object (window in browsers), e.g. if you need to access code across frames or windows
π This is rare and should generally be avoided
Activity
Complete activities 1 and 2 in CodePen. Be sure to fork the pen first if you'd like to save your work to your account!
var fruit = 'apple';
(function basket(){
var fruit = 'banana';
console.log(fruit);
})();
console.log(fruit);
var fruitBasket = {
fruit: 'mango',
getFruit: function(){
console.log(fruit);
fruit = 'orange';
}
};
fruitBasket.getFruit();
console.log(fruit);
What would the following output to the console and why?