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