Mercurial > js4py
annotate core-javascript/tutorial.js @ 115:d99695cdd230 core-javascript
Finish code for transient-bind.
| author | jf3243 |
|---|---|
| date | Wed, 26 Oct 2011 17:45:18 +0100 |
| parents | e39c9c3aa96e |
| children | 31a94b4c8687 |
| rev | line source |
|---|---|
| 109 | 1 var telltail = 'global'; |
| 2 | |
| 3 var TEST = function(title, code){ | |
| 4 print( 'Testing: ' + title ); | |
| 5 code(); | |
| 6 }; | |
| 7 | |
| 8 var assert = function(arg){ | |
| 9 if(!arg){ | |
| 10 ddt(); | |
| 11 } | |
| 12 }; | |
| 13 | |
| 14 '0' == 0 || ddt; | |
| 15 0 == '' || ddt; | |
| 16 '0' != '' || ddt; | |
| 17 NaN != NaN || ddt; | |
| 18 | |
| 19 | |
| 112 | 20 // start closures |
| 21 TEST('closures', function() | |
| 22 { | |
| 113 | 23 // A factory for creating get-set pairs. |
| 112 | 24 var get_set_factory = function(){ |
| 25 | |
| 26 var value; | |
| 27 var get = function(){ | |
| 28 return value; | |
| 29 }; | |
| 30 var set = function(new_value){ | |
| 31 value = new_value; | |
| 32 }; | |
| 33 | |
| 34 return { | |
| 35 get: get, | |
| 36 set: set | |
| 37 }; | |
| 38 }; | |
| 39 | |
| 113 | 40 // Create and unpack two get-set pairs. |
| 112 | 41 var tmp; |
| 42 tmp= get_set_factory(); | |
| 43 var get_a = tmp.get; | |
| 44 var set_a = tmp.set; | |
| 45 | |
| 46 tmp= get_set_factory(); | |
| 47 var get_b = tmp.get; | |
| 48 var set_b = tmp.set; | |
| 49 | |
| 113 | 50 // Test that both pairs work. |
| 112 | 51 set_a(12); |
| 52 assert( get_a() === 12 ); | |
| 53 | |
| 54 set_b(13); | |
| 55 assert( get_b() === 13 ); | |
| 56 | |
| 113 | 57 // Test that each pair has its own value. |
| 58 assert( get_a() === 12 ); | |
| 59 | |
| 112 | 60 }); |
| 61 // end closures | |
| 62 | |
| 63 | |
|
110
f015a6838eb7
Add what-is-this.rst (and include from tutorial.js).
jf3243
parents:
109
diff
changeset
|
64 // start return_this |
| 109 | 65 TEST('return_this', function() |
| 66 { | |
| 67 var return_this = function(){ | |
| 68 return this; | |
| 69 }; | |
| 70 | |
| 71 var default_this = return_this(); | |
| 72 | |
| 73 assert( default_this.telltail === 'global' ); | |
| 74 | |
| 75 var obj = {}; | |
| 76 obj.return_this = return_this; | |
| 77 assert( obj.return_this === return_this ); | |
| 78 | |
| 79 assert( obj.return_this() === obj ); | |
| 80 | |
| 81 assert( return_this.call(obj) === obj ); | |
| 82 assert( return_this.apply(obj) === obj ); | |
| 83 | |
| 84 }); | |
|
110
f015a6838eb7
Add what-is-this.rst (and include from tutorial.js).
jf3243
parents:
109
diff
changeset
|
85 // end return_this |
| 109 | 86 |
| 87 | |
| 114 | 88 // start transient-bind |
| 89 TEST('transient-bind', function() | |
| 90 { | |
| 115 | 91 // Set up the test object. |
| 92 var return_this = function(){ | |
| 93 return this; | |
| 94 }; | |
| 95 var obj = { | |
| 96 return_this : return_this | |
| 97 }; | |
| 98 | |
| 99 // Deferring method call changes the outcome. | |
| 100 var method_deferred = obj.return_this; | |
| 101 assert( method_deferred === obj.return_this ); | |
| 102 assert( method_deferred() !== obj.return_this() ); | |
| 103 | |
| 104 // The two outcomes, precisely stated. | |
| 105 assert( obj.return_this() === obj ); | |
| 106 assert( method_deferred().telltail === 'global' ); | |
| 114 | 107 |
| 108 }); | |
| 109 // end transient-bind | |
| 110 | |
| 111 | |
|
111
73859bf6d639
Add about-new, arguments and millers-device (and include from tutorial.js).
jf3243
parents:
110
diff
changeset
|
112 // start millers-device |
|
73859bf6d639
Add about-new, arguments and millers-device (and include from tutorial.js).
jf3243
parents:
110
diff
changeset
|
113 TEST('millers-device', function() |
| 109 | 114 { |
| 115 var array = []; | |
| 116 var obj = {}; | |
| 117 var fn = function(){}; | |
| 118 | |
| 119 assert( typeof array === 'object' ); | |
| 120 assert( '' + obj === '[object Object]' ); | |
| 121 | |
| 122 assert( obj.toString() === '[object Object]' ); | |
| 123 | |
| 124 var object_toString = obj.toString; | |
| 125 | |
| 126 assert( object_toString.call(array) === '[object Array]' ); | |
| 127 assert( object_toString.call(fn) === '[object Function]' ); | |
| 128 | |
| 129 }); | |
|
111
73859bf6d639
Add about-new, arguments and millers-device (and include from tutorial.js).
jf3243
parents:
110
diff
changeset
|
130 // end millers-device |
| 109 | 131 |
| 132 | |
|
111
73859bf6d639
Add about-new, arguments and millers-device (and include from tutorial.js).
jf3243
parents:
110
diff
changeset
|
133 // start return_arguments |
| 109 | 134 TEST('return_arguments', function() |
| 135 { | |
| 136 var return_arguments = function(){ | |
| 137 return arguments; | |
| 138 }; | |
| 139 | |
| 140 var my_arguments = return_arguments(0, 1); | |
| 141 | |
| 142 assert( my_arguments.length === 2 ); | |
| 143 | |
| 144 assert( my_arguments[0] === 0 ); | |
| 145 assert( my_arguments[1] === 1 ); | |
| 146 | |
| 147 assert( '' + my_arguments === '[object Object]' ); | |
| 148 assert( '' + [0, 1] === '0,1' ); | |
| 149 | |
| 150 var array_slice = [].slice; | |
| 151 var my_arguments_fixed = array_slice.call( my_arguments ); | |
| 152 | |
| 153 assert( '' + my_arguments_fixed === '0,1'); | |
| 154 | |
| 155 var obj = {}; | |
| 156 assert( obj.toString.call( my_arguments_fixed ) === '[object Array]' ); | |
| 157 | |
| 158 }); | |
|
111
73859bf6d639
Add about-new, arguments and millers-device (and include from tutorial.js).
jf3243
parents:
110
diff
changeset
|
159 // end return_arguments |
| 109 | 160 |
| 161 | |
|
111
73859bf6d639
Add about-new, arguments and millers-device (and include from tutorial.js).
jf3243
parents:
110
diff
changeset
|
162 // start new-operator |
|
73859bf6d639
Add about-new, arguments and millers-device (and include from tutorial.js).
jf3243
parents:
110
diff
changeset
|
163 TEST('new-operator', function() |
| 109 | 164 { |
| 165 var this_in_Fn; | |
| 166 | |
| 167 var Fn = function(){ | |
| 168 this_in_Fn = this; | |
| 169 }; | |
| 170 | |
| 171 this_in_Fn = null; | |
| 172 var non_instance = Fn(); | |
| 173 assert( non_instance === undefined ); | |
| 174 assert( this_in_Fn.telltail === 'global'); | |
| 175 | |
| 176 this_in_Fn = null; | |
| 177 var instance = new Fn(); | |
| 178 assert( instance === this_in_Fn ); | |
| 179 assert( instance.telltail === undefined ); | |
| 180 | |
| 181 Fn.prototype.telltail = 'instance-of-Fn'; | |
| 182 assert( instance.telltail === 'instance-of-Fn' ); | |
| 183 | |
| 184 Fn.prototype = { | |
| 185 telltail : 'prototype-is-changed' | |
| 186 }; | |
| 187 var second_instance = new Fn(); | |
| 188 assert( second_instance.telltail === 'prototype-is-changed' ); | |
| 189 | |
| 190 Fn.prototype = second_instance; | |
| 191 second_instance.i = 42; | |
| 192 | |
| 193 var third_instance = new Fn(); | |
| 194 assert( third_instance.telltail === 'prototype-is-changed' ); | |
| 195 assert( third_instance.i === 42 ); | |
| 196 | |
| 197 }); | |
|
111
73859bf6d639
Add about-new, arguments and millers-device (and include from tutorial.js).
jf3243
parents:
110
diff
changeset
|
198 // end new-operator |
