Mercurial > js4py
annotate core-javascript/tutorial.js @ 111:73859bf6d639 core-javascript
Add about-new, arguments and millers-device (and include from tutorial.js).
| author | jf3243 |
|---|---|
| date | Wed, 26 Oct 2011 16:32:34 +0100 |
| parents | f015a6838eb7 |
| children | df92f9f165c7 |
| 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 | |
|
110
f015a6838eb7
Add what-is-this.rst (and include from tutorial.js).
jf3243
parents:
109
diff
changeset
|
20 // start return_this |
| 109 | 21 TEST('return_this', function() |
| 22 { | |
| 23 var return_this = function(){ | |
| 24 return this; | |
| 25 }; | |
| 26 | |
| 27 var default_this = return_this(); | |
| 28 | |
| 29 assert( default_this.telltail === 'global' ); | |
| 30 | |
| 31 var obj = {}; | |
| 32 obj.return_this = return_this; | |
| 33 assert( obj.return_this === return_this ); | |
| 34 | |
| 35 assert( obj.return_this() === obj ); | |
| 36 | |
| 37 assert( return_this.call(obj) === obj ); | |
| 38 assert( return_this.apply(obj) === obj ); | |
| 39 | |
| 40 }); | |
|
110
f015a6838eb7
Add what-is-this.rst (and include from tutorial.js).
jf3243
parents:
109
diff
changeset
|
41 // end return_this |
| 109 | 42 |
| 43 | |
|
111
73859bf6d639
Add about-new, arguments and millers-device (and include from tutorial.js).
jf3243
parents:
110
diff
changeset
|
44 // start millers-device |
|
73859bf6d639
Add about-new, arguments and millers-device (and include from tutorial.js).
jf3243
parents:
110
diff
changeset
|
45 TEST('millers-device', function() |
| 109 | 46 { |
| 47 var array = []; | |
| 48 var obj = {}; | |
| 49 var fn = function(){}; | |
| 50 | |
| 51 assert( typeof array === 'object' ); | |
| 52 assert( '' + obj === '[object Object]' ); | |
| 53 | |
| 54 assert( obj.toString() === '[object Object]' ); | |
| 55 | |
| 56 var object_toString = obj.toString; | |
| 57 | |
| 58 assert( object_toString.call(array) === '[object Array]' ); | |
| 59 assert( object_toString.call(fn) === '[object Function]' ); | |
| 60 | |
| 61 }); | |
|
111
73859bf6d639
Add about-new, arguments and millers-device (and include from tutorial.js).
jf3243
parents:
110
diff
changeset
|
62 // end millers-device |
| 109 | 63 |
| 64 | |
|
111
73859bf6d639
Add about-new, arguments and millers-device (and include from tutorial.js).
jf3243
parents:
110
diff
changeset
|
65 // start return_arguments |
| 109 | 66 TEST('return_arguments', function() |
| 67 { | |
| 68 var return_arguments = function(){ | |
| 69 return arguments; | |
| 70 }; | |
| 71 | |
| 72 var my_arguments = return_arguments(0, 1); | |
| 73 | |
| 74 assert( my_arguments.length === 2 ); | |
| 75 | |
| 76 assert( my_arguments[0] === 0 ); | |
| 77 assert( my_arguments[1] === 1 ); | |
| 78 | |
| 79 assert( '' + my_arguments === '[object Object]' ); | |
| 80 assert( '' + [0, 1] === '0,1' ); | |
| 81 | |
| 82 var array_slice = [].slice; | |
| 83 var my_arguments_fixed = array_slice.call( my_arguments ); | |
| 84 | |
| 85 assert( '' + my_arguments_fixed === '0,1'); | |
| 86 | |
| 87 var obj = {}; | |
| 88 assert( obj.toString.call( my_arguments_fixed ) === '[object Array]' ); | |
| 89 | |
| 90 }); | |
|
111
73859bf6d639
Add about-new, arguments and millers-device (and include from tutorial.js).
jf3243
parents:
110
diff
changeset
|
91 // end return_arguments |
| 109 | 92 |
| 93 | |
|
111
73859bf6d639
Add about-new, arguments and millers-device (and include from tutorial.js).
jf3243
parents:
110
diff
changeset
|
94 // start new-operator |
|
73859bf6d639
Add about-new, arguments and millers-device (and include from tutorial.js).
jf3243
parents:
110
diff
changeset
|
95 TEST('new-operator', function() |
| 109 | 96 { |
| 97 var this_in_Fn; | |
| 98 | |
| 99 var Fn = function(){ | |
| 100 this_in_Fn = this; | |
| 101 }; | |
| 102 | |
| 103 this_in_Fn = null; | |
| 104 var non_instance = Fn(); | |
| 105 assert( non_instance === undefined ); | |
| 106 assert( this_in_Fn.telltail === 'global'); | |
| 107 | |
| 108 this_in_Fn = null; | |
| 109 var instance = new Fn(); | |
| 110 assert( instance === this_in_Fn ); | |
| 111 assert( instance.telltail === undefined ); | |
| 112 | |
| 113 Fn.prototype.telltail = 'instance-of-Fn'; | |
| 114 assert( instance.telltail === 'instance-of-Fn' ); | |
| 115 | |
| 116 Fn.prototype = { | |
| 117 telltail : 'prototype-is-changed' | |
| 118 }; | |
| 119 var second_instance = new Fn(); | |
| 120 assert( second_instance.telltail === 'prototype-is-changed' ); | |
| 121 | |
| 122 Fn.prototype = second_instance; | |
| 123 second_instance.i = 42; | |
| 124 | |
| 125 var third_instance = new Fn(); | |
| 126 assert( third_instance.telltail === 'prototype-is-changed' ); | |
| 127 assert( third_instance.i === 42 ); | |
| 128 | |
| 129 }); | |
|
111
73859bf6d639
Add about-new, arguments and millers-device (and include from tutorial.js).
jf3243
parents:
110
diff
changeset
|
130 // end new-operator |
