diff core-javascript/core-javascript-work/tutorial.js @ 141:e8bfe6c50a01 core-javascript

Add commentary to what-is-new.rst.
author Jonathan Fine <jfine@pytex.org>
date Mon, 31 Oct 2011 21:17:17 +0000
parents abfb176c7346
children
line wrap: on
line diff
--- a/core-javascript/core-javascript-work/tutorial.js	Mon Oct 31 20:41:47 2011 +0000
+++ b/core-javascript/core-javascript-work/tutorial.js	Mon Oct 31 21:17:17 2011 +0000
@@ -168,37 +168,36 @@
 // start new-operator
 TEST('new-operator', function()
 {
+    // A 'traditional class'.  It records 'this' as a side effect.
     var this_in_Fn;
-
     var Fn = function(){
         this_in_Fn = this;
     };
 
-    this_in_Fn = null;
+    // Call the 'class' without the 'new'.
+    this_in_Fn = null;          // Clear any previous value.
     var non_instance = Fn();
     assert( non_instance === undefined );
     assert( this_in_Fn.telltail === 'global');
 
-    this_in_Fn = null;
+    // Call the 'class' with the 'new'.
+    this_in_Fn = null;          // Clear any previous value.
     var instance = new Fn();
     assert( instance === this_in_Fn );
     assert( instance.telltail === undefined );
 
+    // Demonstrate that instance is a descendant of Fn.prototype.
     Fn.prototype.telltail = 'instance-of-Fn';
     assert( instance.telltail === 'instance-of-Fn' );
 
-    Fn.prototype = {
-        telltail : 'prototype-is-changed'
-    };
-    var second_instance = new Fn();
-    assert( second_instance.telltail === 'prototype-is-changed' );
+    // We can give Fn a new prototype.
+    instance.new_telltail = 'child-of-instance';
+    Fn.prototype = instance;
 
-    Fn.prototype = second_instance;
-    second_instance.i = 42;
-
-    var third_instance = new Fn();
-    assert( third_instance.telltail === 'prototype-is-changed' );
-    assert( third_instance.i === 42 );
+    // And now an instance of Fn has two telltails.
+    var second_instance = new Fn();
+    assert( second_instance.telltail === 'instance-of-Fn' );
+    assert( second_instance.new_telltail === 'child-of-instance' );
 
 });
 // end new-operator