js/src/tests/ecma_3/Function/call-001.js

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:9ffb591bddfd
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 /*
7 * Date: 2001-07-13
8 *
9 * SUMMARY: Applying Function.prototype.call to the Function object itself
10 *
11 *
12 * ECMA-262 15.3.4.4 Function.prototype.call (thisArg [,arg1 [,arg2,…] ] )
13 *
14 * When applied to the Function object itself, thisArg should be ignored.
15 * As explained by Waldemar (waldemar@netscape.com):
16 *
17 * Function.call(obj, "print(this)") is equivalent to invoking
18 * Function("print(this)") with this set to obj. Now, Function("print(this)")
19 * is equivalent to new Function("print(this)") (see 15.3.1.1), and the latter
20 * ignores the this value that you passed it and constructs a function
21 * (which we'll call F) which will print the value of the this that will be
22 * passed in when F will be invoked.
23 *
24 * With the last set of () you're invoking F(), which means you're calling it
25 * with no this value. When you don't provide a this value, it defaults to the
26 * global object.
27 *
28 */
29
30 //-----------------------------------------------------------------------------
31 var UBound = 0;
32 var BUGNUMBER = '(none)';
33 var summary = 'Applying Function.prototype.call to the Function object itself';
34 var status = '';
35 var statusitems = [];
36 var actual = '';
37 var actualvalues = [];
38 var expect= '';
39 var expectedvalues = [];
40 var self = this; // capture a reference to the global object
41 var cnOBJECT_GLOBAL = self.toString();
42 var cnOBJECT_OBJECT = (new Object).toString();
43 var cnHello = 'Hello';
44 var cnRed = 'red';
45 var objTEST = {color:cnRed};
46 var f = new Function();
47 var g = new Function();
48
49
50 f = Function.call(self, 'return cnHello');
51 g = Function.call(objTEST, 'return cnHello');
52
53 status = 'Section A of test';
54 actual = f();
55 expect = cnHello;
56 captureThis();
57
58 status = 'Section B of test';
59 actual = g();
60 expect = cnHello;
61 captureThis();
62
63
64 f = Function.call(self, 'return this.toString()');
65 g = Function.call(objTEST, 'return this.toString()');
66
67 status = 'Section C of test';
68 actual = f();
69 expect = cnOBJECT_GLOBAL;
70 captureThis();
71
72 status = 'Section D of test';
73 actual = g();
74 expect = cnOBJECT_GLOBAL;
75 captureThis();
76
77
78 f = Function.call(self, 'return this.color');
79 g = Function.call(objTEST, 'return this.color');
80
81 status = 'Section E of test';
82 actual = f();
83 expect = undefined;
84 captureThis();
85
86 status = 'Section F of test';
87 actual = g();
88 expect = undefined;
89 captureThis();
90
91
92
93 //-----------------------------------------------------------------------------
94 test();
95 //-----------------------------------------------------------------------------
96
97
98 function captureThis()
99 {
100 statusitems[UBound] = status;
101 actualvalues[UBound] = actual;
102 expectedvalues[UBound] = expect;
103 UBound++;
104 }
105
106
107 function test()
108 {
109 enterFunc ('test');
110 printBugNumber(BUGNUMBER);
111 printStatus (summary);
112
113 for (var i = 0; i < UBound; i++)
114 {
115 reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]);
116 }
117
118 exitFunc ('test');
119 }

mercurial