funcbind2.js revision 877:cf4d2252d444
1/* 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/** 25 * Test the functionality of Function.prototype.bind. 26 * 27 * @test 28 * @run 29 */ 30 31function f(a, b) { 32 print("f: this=" + this + ", a=" + a + ", b=" + b); 33} 34function v(a, b) { 35 print("v: this=" + this + ", a=" + a + ", b=" + b + ", c=" + arguments[2]); 36} 37 38(f.bind(null))(); 39(v.bind(null))(); 40 41var boundThis = "boundThis"; 42(f.bind(boundThis))(); 43(v.bind(boundThis))(); 44 45(f.bind(boundThis))("a0"); 46(v.bind(boundThis))("a1"); 47 48(f.bind(boundThis, "a2"))(); 49(v.bind(boundThis, "a3"))(); 50 51(f.bind(boundThis, "a4"))("b4"); 52(v.bind(boundThis, "a5"))("b5"); 53 54(f.bind(boundThis, "a6", "b6"))(); 55(v.bind(boundThis, "a7", "b7"))(); 56 57(f.bind(boundThis, "a8", "b8"))("c8"); // invoking with extra args after all were bound! 58(v.bind(boundThis, "a9", "b9"))("c9"); 59 60(f.bind(boundThis, "a10", "b10", "c10"))(); // binding more args than it receives! 61(v.bind(boundThis, "a11", "b11", "c11"))(); 62 63print("\nTest constructors\n"); 64 65new (f.bind(boundThis))(); 66new (v.bind(boundThis))(); 67 68new (f.bind(boundThis))("a0"); 69new (v.bind(boundThis))("a1"); 70 71new (f.bind(boundThis, "a2"))(); 72new (v.bind(boundThis, "a3"))(); 73 74new (f.bind(boundThis, "a4"))("b4"); 75new (v.bind(boundThis, "a5"))("b5"); 76 77new (f.bind(boundThis, "a6", "b6"))(); 78new (v.bind(boundThis, "a7", "b7"))(); 79 80new (f.bind(boundThis, "a8", "b8"))("c8"); 81new (v.bind(boundThis, "a9", "b9"))("c9"); 82 83new (f.bind(boundThis, "a10", "b10", "c10"))(); 84new (v.bind(boundThis, "a11", "b11", "c11"))(); 85 86print("\nTest double binding\n"); 87 88(f.bind(boundThis).bind("thisIsIgnored"))(); 89new (f.bind("thisIsIgnored").bind("thisIsIgnoredToo"))(); 90new (f.bind("thisIsIgnored", "a12").bind("thisIsIgnoredToo"))(); 91 92(v.bind(boundThis).bind("thisIsIgnored"))(); 93new (v.bind("thisIsIgnored").bind("thisIsIgnoredToo"))(); 94new (v.bind("thisIsIgnored", "a13").bind("thisIsIgnoredToo"))(); 95