1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/tests/ecma_3/Function/call-001.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,119 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +/* 1.10 + * Date: 2001-07-13 1.11 + * 1.12 + * SUMMARY: Applying Function.prototype.call to the Function object itself 1.13 + * 1.14 + * 1.15 + * ECMA-262 15.3.4.4 Function.prototype.call (thisArg [,arg1 [,arg2,…] ] ) 1.16 + * 1.17 + * When applied to the Function object itself, thisArg should be ignored. 1.18 + * As explained by Waldemar (waldemar@netscape.com): 1.19 + * 1.20 + * Function.call(obj, "print(this)") is equivalent to invoking 1.21 + * Function("print(this)") with this set to obj. Now, Function("print(this)") 1.22 + * is equivalent to new Function("print(this)") (see 15.3.1.1), and the latter 1.23 + * ignores the this value that you passed it and constructs a function 1.24 + * (which we'll call F) which will print the value of the this that will be 1.25 + * passed in when F will be invoked. 1.26 + * 1.27 + * With the last set of () you're invoking F(), which means you're calling it 1.28 + * with no this value. When you don't provide a this value, it defaults to the 1.29 + * global object. 1.30 + * 1.31 + */ 1.32 + 1.33 +//----------------------------------------------------------------------------- 1.34 +var UBound = 0; 1.35 +var BUGNUMBER = '(none)'; 1.36 +var summary = 'Applying Function.prototype.call to the Function object itself'; 1.37 +var status = ''; 1.38 +var statusitems = []; 1.39 +var actual = ''; 1.40 +var actualvalues = []; 1.41 +var expect= ''; 1.42 +var expectedvalues = []; 1.43 +var self = this; // capture a reference to the global object 1.44 +var cnOBJECT_GLOBAL = self.toString(); 1.45 +var cnOBJECT_OBJECT = (new Object).toString(); 1.46 +var cnHello = 'Hello'; 1.47 +var cnRed = 'red'; 1.48 +var objTEST = {color:cnRed}; 1.49 +var f = new Function(); 1.50 +var g = new Function(); 1.51 + 1.52 + 1.53 +f = Function.call(self, 'return cnHello'); 1.54 +g = Function.call(objTEST, 'return cnHello'); 1.55 + 1.56 +status = 'Section A of test'; 1.57 +actual = f(); 1.58 +expect = cnHello; 1.59 +captureThis(); 1.60 + 1.61 +status = 'Section B of test'; 1.62 +actual = g(); 1.63 +expect = cnHello; 1.64 +captureThis(); 1.65 + 1.66 + 1.67 +f = Function.call(self, 'return this.toString()'); 1.68 +g = Function.call(objTEST, 'return this.toString()'); 1.69 + 1.70 +status = 'Section C of test'; 1.71 +actual = f(); 1.72 +expect = cnOBJECT_GLOBAL; 1.73 +captureThis(); 1.74 + 1.75 +status = 'Section D of test'; 1.76 +actual = g(); 1.77 +expect = cnOBJECT_GLOBAL; 1.78 +captureThis(); 1.79 + 1.80 + 1.81 +f = Function.call(self, 'return this.color'); 1.82 +g = Function.call(objTEST, 'return this.color'); 1.83 + 1.84 +status = 'Section E of test'; 1.85 +actual = f(); 1.86 +expect = undefined; 1.87 +captureThis(); 1.88 + 1.89 +status = 'Section F of test'; 1.90 +actual = g(); 1.91 +expect = undefined; 1.92 +captureThis(); 1.93 + 1.94 + 1.95 + 1.96 +//----------------------------------------------------------------------------- 1.97 +test(); 1.98 +//----------------------------------------------------------------------------- 1.99 + 1.100 + 1.101 +function captureThis() 1.102 +{ 1.103 + statusitems[UBound] = status; 1.104 + actualvalues[UBound] = actual; 1.105 + expectedvalues[UBound] = expect; 1.106 + UBound++; 1.107 +} 1.108 + 1.109 + 1.110 +function test() 1.111 +{ 1.112 + enterFunc ('test'); 1.113 + printBugNumber(BUGNUMBER); 1.114 + printStatus (summary); 1.115 + 1.116 + for (var i = 0; i < UBound; i++) 1.117 + { 1.118 + reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]); 1.119 + } 1.120 + 1.121 + exitFunc ('test'); 1.122 +}