From b34fcb0cd995c117875edd3d0d7473d57eb82eec Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Fri, 2 Jan 2015 14:33:51 +1100 Subject: [PATCH] split up method binding and add description for memoization assignment operator --- doc/playground.md | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/doc/playground.md b/doc/playground.md index 16acafde89..985bc5409d 100644 --- a/doc/playground.md +++ b/doc/playground.md @@ -18,11 +18,19 @@ to5.transform("code", { playground: true }); * [Memoization operator](#memoization-operator) * [Method binding](#method-binding) + * [Method binding function shorthand](#method-binding-function-shorthand) * [Object getter memoization](#object-getter-memoization) * [This shorthand](#this-shorthand) ### Memoization assignment operator +The memoization assignment operator allows you to lazily set an object property. +It checks whether there's a property defined on the object and if there isn't then +the right hand value is set. + +This means that `obj.x` in the following `var x = { x: undefined }; obj.x ?= 2;` +will still be `undefined` because it's already been defined on the object. + ```javascript var obj = {}; obj.x ?= 2; @@ -42,7 +50,7 @@ var obj = {}; obj.x ?= 2; ``` -equivalent to: +equivalent to ```javascript var obj = {}; @@ -54,17 +62,25 @@ if (!Object.prototype.hasOwnProperty.call(obj, "x")) obj.x = 2; ```javascript var fn = obj#method; var fn = obj#method("foob"); - -["foo", "bar"].map(#toUpperCase); // ["FOO", "BAR"] -[1.1234, 23.53245, 3].map(#toFixed(2)); // ["1.12", "23.53", "3.00"] ``` -equivalent to: +equivalent to ```javascript var fn = obj.method.bind(obj); var fn = obj.method.bind(obj, "foob"); +``` +### Method binding function shorthand + +```javascript +["foo", "bar"].map(#toUpperCase); // ["FOO", "BAR"] +[1.1234, 23.53245, 3].map(#toFixed(2)); // ["1.12", "23.53", "3.00"] +``` + +equivalent to + +```javascript ["foo", "bar"].map(function (val) { return val.toUpperCase(); }); [1.1234, 23.53245, 3].map(function (val) { return val.toFixed(2); }); ```