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