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