Mercurial > js4py
annotate core-javascript/tutorial.js @ 114:e39c9c3aa96e core-javascript
Add stub doc and test for Bind is transient.
| author | jf3243 |
|---|---|
| date | Wed, 26 Oct 2011 17:33:12 +0100 |
| parents | e73b36990587 |
| children | d99695cdd230 |
| 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 { | |
| 91 | |
| 92 }); | |
| 93 // end transient-bind | |
| 94 | |
| 95 | |
|
111
73859bf6d639
Add about-new, arguments and millers-device (and include from tutorial.js).
jf3243
parents:
110
diff
changeset
|
96 // start millers-device |
|
73859bf6d639
Add about-new, arguments and millers-device (and include from tutorial.js).
jf3243
parents:
110
diff
changeset
|
97 TEST('millers-device', function() |
| 109 | 98 { |
| 99 var array = []; | |
| 100 var obj = {}; | |
| 101 var fn = function(){}; | |
| 102 | |
| 103 assert( typeof array === 'object' ); | |
| 104 assert( '' + obj === '[object Object]' ); | |
| 105 | |
| 106 assert( obj.toString() === '[object Object]' ); | |
| 107 | |
| 108 var object_toString = obj.toString; | |
| 109 | |
| 110 assert( object_toString.call(array) === '[object Array]' ); | |
| 111 assert( object_toString.call(fn) === '[object Function]' ); | |
| 112 | |
| 113 }); | |
|
111
73859bf6d639
Add about-new, arguments and millers-device (and include from tutorial.js).
jf3243
parents:
110
diff
changeset
|
114 // end millers-device |
| 109 | 115 |
| 116 | |
|
111
73859bf6d639
Add about-new, arguments and millers-device (and include from tutorial.js).
jf3243
parents:
110
diff
changeset
|
117 // start return_arguments |
| 109 | 118 TEST('return_arguments', function() |
| 119 { | |
| 120 var return_arguments = function(){ | |
| 121 return arguments; | |
| 122 }; | |
| 123 | |
| 124 var my_arguments = return_arguments(0, 1); | |
| 125 | |
| 126 assert( my_arguments.length === 2 ); | |
| 127 | |
| 128 assert( my_arguments[0] === 0 ); | |
| 129 assert( my_arguments[1] === 1 ); | |
| 130 | |
| 131 assert( '' + my_arguments === '[object Object]' ); | |
| 132 assert( '' + [0, 1] === '0,1' ); | |
| 133 | |
| 134 var array_slice = [].slice; | |
| 135 var my_arguments_fixed = array_slice.call( my_arguments ); | |
| 136 | |
| 137 assert( '' + my_arguments_fixed === '0,1'); | |
| 138 | |
| 139 var obj = {}; | |
| 140 assert( obj.toString.call( my_arguments_fixed ) === '[object Array]' ); | |
| 141 | |
| 142 }); | |
|
111
73859bf6d639
Add about-new, arguments and millers-device (and include from tutorial.js).
jf3243
parents:
110
diff
changeset
|
143 // end return_arguments |
| 109 | 144 |
| 145 | |
|
111
73859bf6d639
Add about-new, arguments and millers-device (and include from tutorial.js).
jf3243
parents:
110
diff
changeset
|
146 // start new-operator |
|
73859bf6d639
Add about-new, arguments and millers-device (and include from tutorial.js).
jf3243
parents:
110
diff
changeset
|
147 TEST('new-operator', function() |
| 109 | 148 { |
| 149 var this_in_Fn; | |
| 150 | |
| 151 var Fn = function(){ | |
| 152 this_in_Fn = this; | |
| 153 }; | |
| 154 | |
| 155 this_in_Fn = null; | |
| 156 var non_instance = Fn(); | |
| 157 assert( non_instance === undefined ); | |
| 158 assert( this_in_Fn.telltail === 'global'); | |
| 159 | |
| 160 this_in_Fn = null; | |
| 161 var instance = new Fn(); | |
| 162 assert( instance === this_in_Fn ); | |
| 163 assert( instance.telltail === undefined ); | |
| 164 | |
| 165 Fn.prototype.telltail = 'instance-of-Fn'; | |
| 166 assert( instance.telltail === 'instance-of-Fn' ); | |
| 167 | |
| 168 Fn.prototype = { | |
| 169 telltail : 'prototype-is-changed' | |
| 170 }; | |
| 171 var second_instance = new Fn(); | |
| 172 assert( second_instance.telltail === 'prototype-is-changed' ); | |
| 173 | |
| 174 Fn.prototype = second_instance; | |
| 175 second_instance.i = 42; | |
| 176 | |
| 177 var third_instance = new Fn(); | |
| 178 assert( third_instance.telltail === 'prototype-is-changed' ); | |
| 179 assert( third_instance.i === 42 ); | |
| 180 | |
| 181 }); | |
|
111
73859bf6d639
Add about-new, arguments and millers-device (and include from tutorial.js).
jf3243
parents:
110
diff
changeset
|
182 // end new-operator |
