From 7fdd21b26dfc65f3973c18d9408bd8ae52f95bf4 Mon Sep 17 00:00:00 2001 From: Gabe Hollombe Date: Sun, 10 Jun 2012 11:19:54 +0700 Subject: [PATCH] Add example of destructuring assignment in class constructors for options --- .../coffee/constructor_destructuring.coffee | 4 +++ documentation/index.html.erb | 5 +++ documentation/js/constructor_destructuring.js | 12 +++++++ index.html | 31 +++++++++++++++++++ 4 files changed, 52 insertions(+) create mode 100644 documentation/coffee/constructor_destructuring.coffee create mode 100644 documentation/js/constructor_destructuring.js diff --git a/documentation/coffee/constructor_destructuring.coffee b/documentation/coffee/constructor_destructuring.coffee new file mode 100644 index 0000000000..53c4b39a0d --- /dev/null +++ b/documentation/coffee/constructor_destructuring.coffee @@ -0,0 +1,4 @@ +class Person + constructor: (options) -> + {@name, @age, @height} = options + diff --git a/documentation/index.html.erb b/documentation/index.html.erb index 522ad10849..54714205cf 100644 --- a/documentation/index.html.erb +++ b/documentation/index.html.erb @@ -761,6 +761,11 @@ Expressions Destructuring assignment can even be combined with splats.

<%= code_for('patterns_and_splats', 'contents.join("")') %> +

+ Destructuring assignment is also useful when combined with class constructors + to assign propeties to your instance from an options object passed to the constructor. +

+ <%= code_for('constructor_destructuring', 'contents.join("")') %>

diff --git a/documentation/js/constructor_destructuring.js b/documentation/js/constructor_destructuring.js new file mode 100644 index 0000000000..06c7d4877e --- /dev/null +++ b/documentation/js/constructor_destructuring.js @@ -0,0 +1,12 @@ +// Generated by CoffeeScript 1.3.3 +var Person; + +Person = (function() { + + function Person(options) { + this.name = options.name, this.age = options.age, this.height = options.height; + } + + return Person; + +})(); diff --git a/index.html b/index.html index 426cc0401b..95d299aa97 100644 --- a/index.html +++ b/index.html @@ -1735,6 +1735,37 @@

tag = ""; _ref = tag.split(""), open = _ref[0], contents = 3 <= _ref.length ? __slice.call(_ref, 1, _i = _ref.length - 1) : (_i = 1, []), close = _ref[_i++]; +;alert(contents.join(""));'>run: contents.join("")
+

+ Destructuring assignment is also useful when combined with class constructors + to assign propeties to your instance from an options object passed to the constructor. +

+
class Person
+  constructor: (options) -> 
+    {@name, @age, @height} = options
+
+
var Person;
+
+Person = (function() {
+
+  function Person(options) {
+    this.name = options.name, this.age = options.age, this.height = options.height;
+  }
+
+  return Person;
+
+})();
+
load
run: contents.join("")