gjs r139 - trunk/doc



Author: johan
Date: Fri Jan  9 19:02:43 2009
New Revision: 139
URL: http://svn.gnome.org/viewvc/gjs?rev=139&view=rev

Log:
Recommend Lang.bind instead of 'let me = this;'. Update examples.

Modified:
   trunk/doc/Style_Guide.txt

Modified: trunk/doc/Style_Guide.txt
==============================================================================
--- trunk/doc/Style_Guide.txt	(original)
+++ trunk/doc/Style_Guide.txt	Fri Jan  9 19:02:43 2009
@@ -54,11 +54,29 @@
 the value of this where the closure is created, because "this" is a keyword with a value passed 
 in at function invocation time, it is not a variable that can be captured in closures.
 
-To solve this, you create a temporary "me" variable:
+To solve this, use Lang.bind, eg:
 
 <pre>
-let me = this;
-let myClosure = function() { me.frobate() };
+const Lang = imports.lang;
+
+let closure = Lang.bind(this, function() { this._fnorbate() });
+</pre>
+
+A more realistic example would be connecting to a signal on a
+method of a prototype:
+
+<pre>
+const Lang = imports.lang;
+
+MyPrototype = {
+    _init : function() {
+       fnorb.connect('frobate', Lang.bind(this, this._onFnorbFrobate));
+    },
+
+    _onFnorbFrobate : function(fnorb) {
+       this._updateFnorb();
+    },    
+};
 </pre>
 
 == Object literal syntax ==



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]