1// Copyright JS Foundation and other contributors, http://js.foundation 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15var mul = function(a, b) { 16 return a * b; 17}; 18 19var triple = mul.bind(null, 3); 20delete mul; 21assert (triple(20) === 60); 22assert (triple.prototype === undefined); 23 24var dupl = triple.bind({}, 2); 25assert (dupl() === 6); 26assert (dupl.prototype === undefined); 27 28try { 29 var obj = {}; 30 var new_func = obj.bind(null, 'foo'); 31 assert (false); 32} catch (e) { 33 assert (e instanceof TypeError); 34} 35 36var obj1 = {num : 36}; 37 38var f1 = function(a) { 39 return this.num + a; 40} 41 42var add36 = f1.bind(obj1); 43assert (add36(24) === 60); 44 45var appendfoo = f1.bind(obj1, "foo"); 46assert (appendfoo() === "36foo"); 47 48var f2 = function(a) { 49 return this.num + a.num; 50} 51 52var sum = f2.bind(obj1, obj1); 53assert (sum() === 72); 54 55function P(x, y) { 56 this.x = x; 57 this.y = y; 58} 59 60var P1 = P.bind({}, 2); 61var _p1 = new P1(); 62assert (_p1.x === 2); 63assert (_p1.y === undefined); 64assert (_p1 instanceof P); 65assert (_p1 instanceof P1); 66 67var P2 = P1.bind(null); 68var _p2 = new P2(); 69assert (_p2.x === 2); 70assert (_p2.y === undefined); 71 72_p2 = new P2(12, 60); 73assert (_p2.x === 2); 74assert (_p2.y === 12); 75 76_p2 = new P2({}, 12); 77assert (_p2.x === 2); 78assert (Object.getPrototypeOf(_p2.y) === Object.prototype); 79assert (_p2 instanceof P); 80assert (_p2 instanceof P1); 81assert (_p2 instanceof P2); 82 83var P3 = P2.bind({}, 5); 84var _p3 = new P3(8); 85assert (_p3.x === 2); 86assert (_p3.y === 5); 87assert (_p3 instanceof P); 88assert (_p3 instanceof P1); 89assert (_p3 instanceof P2); 90assert (_p3 instanceof P3); 91 92var P4 = P.bind(); 93P4(4, 5); 94assert (x === 4); 95assert (y === 5); 96 97var _x = x; 98var _y = y; 99 100var P5 = P.bind(undefined); 101P5(5, 4); 102assert (x === _y); 103assert (y === _x); 104 105var number = Number.constructor; 106var bound = number.bind(null, 24); 107var foo = new bound(); 108assert (foo() === undefined); 109 110var number = Number; 111var bound = number.bind(null, 3); 112var foo = new bound(); 113assert (foo == 3); 114assert (foo instanceof Number); 115assert (foo.prototype === undefined); 116 117var func = Number.prototype.toString.bind('foo'); 118assert (func instanceof Function); 119 120try { 121 var math = Math.sin; 122 var bound = math.bind(null, 0); 123 var foo = new bound(); 124 assert (false); 125} catch (e) { 126 assert (e instanceof TypeError); 127} 128 129var foo = function(x, y) { } 130 131var bound = foo.bind(null); 132assert(bound.length === 2); 133 134bound = foo.bind(null, 9); 135assert(bound.length === 1); 136 137bound = foo.bind(null, 9, 8); 138assert(bound.length === 0); 139 140