Mercurial > js4py
annotate core-javascript/core-javascript-work/tutorial.js @ 135:f9f6684f9b32 core-javascript
$ hg mv tutorial.js core-javascript-work/ # and amend the rst files also
| author | jf3243 |
|---|---|
| date | Mon, 31 Oct 2011 11:07:46 +0000 |
| parents | core-javascript/tutorial.js@31a94b4c8687 |
| children | 20d76c716c9d |
| 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 }; | |
| 134 | 70 |
| 109 | 71 var default_this = return_this(); |
| 72 assert( default_this.telltail === 'global' ); | |
| 73 | |
| 74 var obj = {}; | |
| 75 obj.return_this = return_this; | |
| 76 assert( obj.return_this === return_this ); | |
| 77 | |
| 78 assert( obj.return_this() === obj ); | |
| 134 | 79 assert( obj['return_this']() === obj ); |
| 109 | 80 |
| 81 assert( return_this.call(obj) === obj ); | |
| 82 assert( return_this.apply(obj) === obj ); | |
| 83 | |
| 134 | 84 assert( return_this.call(undefined).telltail === 'global' ); |
| 85 assert( return_this.apply(undefined).telltail === 'global' ); | |
| 86 | |
| 109 | 87 }); |
|
110
f015a6838eb7
Add what-is-this.rst (and include from tutorial.js).
jf3243
parents:
109
diff
changeset
|
88 // end return_this |
| 109 | 89 |
| 90 | |
| 114 | 91 // start transient-bind |
| 92 TEST('transient-bind', function() | |
| 93 { | |
| 115 | 94 // Set up the test object. |
| 95 var return_this = function(){ | |
| 96 return this; | |
| 97 }; | |
| 98 var obj = { | |
| 99 return_this : return_this | |
| 100 }; | |
| 101 | |
| 102 // Deferring method call changes the outcome. | |
| 103 var method_deferred = obj.return_this; | |
| 104 assert( method_deferred === obj.return_this ); | |
| 105 assert( method_deferred() !== obj.return_this() ); | |
| 106 | |
| 107 // The two outcomes, precisely stated. | |
| 108 assert( obj.return_this() === obj ); | |
| 109 assert( method_deferred().telltail === 'global' ); | |
| 114 | 110 |
| 111 }); | |
| 112 // end transient-bind | |
| 113 | |
| 114 | |
|
111
73859bf6d639
Add about-new, arguments and millers-device (and include from tutorial.js).
jf3243
parents:
110
diff
changeset
|
115 // start millers-device |
|
73859bf6d639
Add about-new, arguments and millers-device (and include from tutorial.js).
jf3243
parents:
110
diff
changeset
|
116 TEST('millers-device', function() |
| 109 | 117 { |
| 118 var array = []; | |
| 119 var obj = {}; | |
| 120 var fn = function(){}; | |
| 121 | |
| 122 assert( typeof array === 'object' ); | |
| 123 assert( '' + obj === '[object Object]' ); | |
| 124 | |
| 125 assert( obj.toString() === '[object Object]' ); | |
| 126 | |
| 127 var object_toString = obj.toString; | |
| 128 | |
| 129 assert( object_toString.call(array) === '[object Array]' ); | |
| 130 assert( object_toString.call(fn) === '[object Function]' ); | |
| 131 | |
| 132 }); | |
|
111
73859bf6d639
Add about-new, arguments and millers-device (and include from tutorial.js).
jf3243
parents:
110
diff
changeset
|
133 // end millers-device |
| 109 | 134 |
| 135 | |
|
111
73859bf6d639
Add about-new, arguments and millers-device (and include from tutorial.js).
jf3243
parents:
110
diff
changeset
|
136 // start return_arguments |
| 109 | 137 TEST('return_arguments', function() |
| 138 { | |
| 139 var return_arguments = function(){ | |
| 140 return arguments; | |
| 141 }; | |
| 142 | |
| 143 var my_arguments = return_arguments(0, 1); | |
| 144 | |
| 145 assert( my_arguments.length === 2 ); | |
| 146 | |
| 147 assert( my_arguments[0] === 0 ); | |
| 148 assert( my_arguments[1] === 1 ); | |
| 149 | |
| 150 assert( '' + my_arguments === '[object Object]' ); | |
| 151 assert( '' + [0, 1] === '0,1' ); | |
| 152 | |
| 153 var array_slice = [].slice; | |
| 154 var my_arguments_fixed = array_slice.call( my_arguments ); | |
| 155 | |
| 156 assert( '' + my_arguments_fixed === '0,1'); | |
| 157 | |
| 158 var obj = {}; | |
| 159 assert( obj.toString.call( my_arguments_fixed ) === '[object Array]' ); | |
| 160 | |
| 161 }); | |
|
111
73859bf6d639
Add about-new, arguments and millers-device (and include from tutorial.js).
jf3243
parents:
110
diff
changeset
|
162 // end return_arguments |
| 109 | 163 |
| 164 | |
|
111
73859bf6d639
Add about-new, arguments and millers-device (and include from tutorial.js).
jf3243
parents:
110
diff
changeset
|
165 // start new-operator |
|
73859bf6d639
Add about-new, arguments and millers-device (and include from tutorial.js).
jf3243
parents:
110
diff
changeset
|
166 TEST('new-operator', function() |
| 109 | 167 { |
| 168 var this_in_Fn; | |
| 169 | |
| 170 var Fn = function(){ | |
| 171 this_in_Fn = this; | |
| 172 }; | |
| 173 | |
| 174 this_in_Fn = null; | |
| 175 var non_instance = Fn(); | |
| 176 assert( non_instance === undefined ); | |
| 177 assert( this_in_Fn.telltail === 'global'); | |
| 178 | |
| 179 this_in_Fn = null; | |
| 180 var instance = new Fn(); | |
| 181 assert( instance === this_in_Fn ); | |
| 182 assert( instance.telltail === undefined ); | |
| 183 | |
| 184 Fn.prototype.telltail = 'instance-of-Fn'; | |
| 185 assert( instance.telltail === 'instance-of-Fn' ); | |
| 186 | |
| 187 Fn.prototype = { | |
| 188 telltail : 'prototype-is-changed' | |
| 189 }; | |
| 190 var second_instance = new Fn(); | |
| 191 assert( second_instance.telltail === 'prototype-is-changed' ); | |
| 192 | |
| 193 Fn.prototype = second_instance; | |
| 194 second_instance.i = 42; | |
| 195 | |
| 196 var third_instance = new Fn(); | |
| 197 assert( third_instance.telltail === 'prototype-is-changed' ); | |
| 198 assert( third_instance.i === 42 ); | |
| 199 | |
| 200 }); | |
|
111
73859bf6d639
Add about-new, arguments and millers-device (and include from tutorial.js).
jf3243
parents:
110
diff
changeset
|
201 // end new-operator |
