How to Setup Babel es2015 Javascript on Windows 8.1?

First create a folder:
Then CD into that folder through CMD
Then do the follow in command-line

npm init
npm install -g babel-cli
npm install babel-preset-es2015–save-dev

Then create app.js.es6 file

Then use this example code:

class Person {
	constructor(name) {
		this.name = name;
	}
	greet(){
		return "Hello I am ${this.name}";
	}
}

let person = new Person("Tyler");
console.log(person.greet());

babel app.js.es6 –presets2015 | clip

Clip copies the string from commandline. Then you should get the follow code output:

"use strict";

var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var Person = (function () {
	function Person(name) {
		_classCallCheck(this, Person);

		this.name = name;
	}

	_createClass(Person, [{
		key: "greet",
		value: function greet() {
			return "Hello I am ${this.name}";
		}
	}]);

	return Person;
})();

var person = new Person("Tyler");
console.log(person.greet());

Now we want to create an index.html file

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title></title>
</head>
<body>
	<h2>ES2015</h2>

	<script src="app.js"></script>
</body>
</html>

babel app.js.es6 –presets es2015 > 5

Now we are going to create a file called .bablrc this file will shorten the command to compile the babel file

{
	presets: ["es2015"]
}

Next we type in this code to make compiling es2015 javascript faster.

babel app.js.es6 > app.js

babel_CMD_setup

https://github.com/JasonDeving/babel2015windowssetup

javascript – namespacing, public and private data

This is quick code sample of public and private data using javascript. You would create anonymous call back function. Private data would be store in var.
Public data would be returned.

// we create anonymous callback function to create private and public functions
var THINGS = function() {
// private
var gifts = 3;
// public
return {
    gems: 1000,
    gold: 2000,
    swords: 300,
    SECRET: {
      open: function() {
        gifts--;
        alert('OPENED!');
      }
    }
};}();

I hope you found this code snippet useful in your coding. These are higher order examples of code has to deal with object orientated programming. You can apply these concepts elsewhere.

Public functions can be access and changed anywhere. Where as private can’t be changed the numbers stay static. For example gifts will stay 3, but the public function can subtract it in increments of negative 1.

The reason behind public and private is track bugs. If you have too many public/global variables you get lost quick and other variables can overwrite those current variables, which is something you don’t want. Plus there is name spacing here the namespace is THINGS.

Javascript – Sales Tax Program

function tax (price, percent) {
		return parseFloat((price*percent/100).toFixed(2));
	}
	var randomItem = 9.85;
	var salesTax = 7.5;
	var total = randomItem + tax(randomItem, salesTax);
	console.log(total);

 

This is a program on how to calculate sales tax with javascript, because handling floats is a bit weird with javascript.

How to Benchmark Your Javascript?

This is a quick tutorial on how to benchmark your javascript with console.time(“arg”) and console.timeEnd(“arg”)
You would use developer tools to see how fast your script runs.
javascript_benchmark

Example Code:

Array.prototype.proto1 = function(){};
Array.prototype.proto2 = function(){};
Array.prototype.proto3 = function(){};
Array.prototype.proto4 = function(){};
Array.prototype.proto5 = function(){};

var objectLateral = {
  arrayBank: ['Some random string', 'Other String', 'More Stringy Spaghetti']
};

function runThisCode(){
  var things = objectLateral.arrayBank,
      list = "";
	console.time("time");  

  for(var i = 0, ff = things.length; i < ff; i++){
    list += (things[i] + " ");
  }

  console.timeEnd("time");
  return list.trim();
}

runThisCode();