Skip to content

Instantly share code, notes, and snippets.

@case-eee

case-eee/es5.md Secret

Created March 28, 2017 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save case-eee/54c79fd84445e24cb017b6de17e455fa to your computer and use it in GitHub Desktop.
Save case-eee/54c79fd84445e24cb017b6de17e455fa to your computer and use it in GitHub Desktop.
es5

String Interpolation

var customer = { name: "Foo" };
var card = { amount: 7, product: "Bar", unitprice: 42 };
var message = "Hello " + customer.name + ",\n" +
"want to buy " + card.amount + " " + card.product + " for\n" +
"a total of " + (card.amount * card.unitprice) + " bucks?";

Arrow Syntax

odds = evens.map(function (v) { 
  return v + 1; 
});

pairs = evens.map(function (v) {   
  return { even: v, odd: v + 1 }; 
});

nums = evens.map(function (v, i) { 
  return v + i; 
});

Arrow Syntax & This

var _this = this
$('.btn').click(function(event){
  _this.sendData()
})

Default Parameters

function addNumbers (x, y, z) {
    if (y === undefined) {
        y = 7;
    }
    if (z === undefined) {
        z = 42;
    }
    return x + y + z;
};

addNumbers(1) === 50;

Classes & Prototypes

function Shape (id, x, y) {
    this.id = id;
    this.move(x, y);
};

Shape.prototype.move = function (x, y) {
    this.x = x;
    this.y = y;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment