JaxNode November 2015
Let and Const
Iterators and Generators
Arrow => functions
Reflection
New literals
Template Strings
Classes
Lexical Scoping
Maps, Set, WeakMap
ArrayBuffer
Promises
Octal support
"use strict";
class MeetupGroup {
constructor(name) {
this.name = name;
}
Say() {
// Notice use of string templates.
console.log(`This the ${this.name} meetup group!`);
}
}
class JavaScriptGroup extends MeetupGroup {
Say() {
super.Say()
console.log('And is a JavaScript User Group.');
}
}
let meetup = new MeetupGroup('JaxNode');
meetup.Say();
"use strict";
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var MeetupGroup = (function () {
function MeetupGroup(name) {
this.name = name;
}
MeetupGroup.prototype.Say = function () {
console.log("This the " + this.name + " meetup group!");
};
return MeetupGroup;
})();
var JavaScriptGroup = (function (_super) {
__extends(JavaScriptGroup, _super);
function JavaScriptGroup() {
_super.apply(this, arguments);
}
JavaScriptGroup.prototype.Say = function () {
_super.prototype.Say.call(this);
console.log('And is a JavaScript User Group.');
};
return JavaScriptGroup;
})(MeetupGroup);
var meetup = new MeetupGroup('JaxNode');
meetup.Say();
"use strict";
let arr = [3, 5, 7];
arr.foo = "hello";
for (let i in arr) {
console.log(i); // logs "0", "1", "2", "foo"
}
for (let i of arr) {
console.log(i); // logs "3", "5", "7"
}
arr.forEach(item => {
console.log(item);
});
"use strict";
varTest();
letTest();
function varTest() {
var x = 31;
if (true) {
var x = 71; // same variable!
console.log(x); // 71
}
console.log(x); // 71
}
function letTest() {
let x = 31;
if (true) {
let x = 71; // different variable
console.log(x); // 71
}
console.log(x); // 31
}
const bar = 'Immutable';
console.log(bar);
bar = 'mutable'; // Will not throw an error unless you use use strict, but will keep original value.
console.log(bar);
var mySet = new WeakSet();
var c1 = { foo: "bar" };
var c2 = {};
var c3 = {};
mySet.add(c1);
mySet.add(c2);
console.log(mySet.has(c2)); // true
console.log(mySet.has(c3)); // false, foo has not been added to the set
mySet.delete(c2); // removes window from the set
console.log(mySet.has(c2)); // false, window has been removed
var myMap = new WeakMap();
var a1 = {};
var b1 = {};
myMap.set(a1, 'foo');
console.log(myMap.get(a1));
console.log(myMap.has(a1));
console.log(myMap.has(b1));
myMap.delete(a1);
console.log(myMap.has(a1));
"use strict";
function *getSomeValues() {
yield 1;
yield 2;
yield 3;
yield 4;
yield 5;
}
var someValues = getSomeValues();
console.log(someValues.next()); // returns { value: 1, done: false }
console.log(someValues.next()); // returns { value: 2, done: false }
console.log(someValues.next()); // returns { value: 3, done: false }
console.log(someValues.next()); // returns { value: 4, done: false }
console.log(someValues.next()); // returns { value: 5, done: false }
console.log(someValues.next()); // returns { value: undefined, done: true }
"use strict";
// Without promises
var fs = require('fs'),
rootPath = 'C:\\Users\\dfekke\\Documents\\',
ext = 'tiff',
newExt = 'TIF';
fs.readdir(rootPath,
function(err, files) {
if (err) {
console.error("unable to read directory");
} else {
var re = new RegExp("^.*\\." + ext + "$");
for (var file in files) {
if (re.test(files[file])) {
console.log(files[file]);
var oldPath = rootPath + files[file];
var newPath = rootPath + files[file].substring(0, files[file].length - ext.length) + newExt;
console.log(newPath);
fs.rename(oldPath,
newPath,
function (err) {
console.error("unable to rename file");
});
}
}
}
});
"use strict";
// With promises
var fs = require('fs'),
Promise = require("bluebird"),
rootPath = 'C:\\Users\\dfekke\\Documents\\',
ext = 'tiff',
newExt = 'TIF';
Promise.promisifyAll(fs);
fs.readdirAsync(rootPath).then((files) => {
var re = new RegExp("^.*\\." + ext + "$");
files.forEach(file => {
if (re.test(file)) {
console.log(file);
var oldPath = rootPath + file;
var newPath = rootPath + file.substring(0, file.length - ext.length) + newExt;
console.log(newPath);
fs.renameAsync(oldPath, newPath).catch(err => {
console.error("unable to rename file");
});
}
});
}).catch((err) => {
console.error("unable to read directory");
});
"use strict";
let printName = name => console.log(`Your user group name is ${name}`);
printName('JaxNode');
// Lexical this from Babel documentation.
var bob = {
_name: "Bob",
_friends: ['Tommy', 'Richard', 'Susan'],
printFriends1() {
// Here we hoist the this object into that so we can use it in the closure function.
var that = this;
this._friends.forEach(function (friend, index){
console.log(`${that._name} knows ${friend}`);
});
},
printFriends2() {
// Here there is no need to hoist the this object because it can be scoped in the lambda.
this._friends.forEach(f =>
console.log(`${this._name} knows ${f}`));
}
};
bob.printFriends1();
bob.printFriends2();
"use strict";
//Available in Node 5.0
// Merging arrays
// Code with spread operator
let inner = [3, 4];
let merged = [0, 1, 2, ...inner, 5]; //Spread operator will fail if run in Node 4.X
console.log(merged);
function Foo() {
if (!new.target) throw "Foo() must be called with new";
console.log("Foo instantiated with new");
}
Foo(); // throws "Foo() must be called with new"
new Foo(); // logs "Foo instantiated with new"