Debian Codegraph @

rhino/testsrc/doctests/function.bind.doctest

raw
1 // This Source Code Form is subject to the terms of the Mozilla Public
2 // License, v. 2.0. If a copy of the MPL was not distributed with this
3 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5 js> load('testsrc/doctests/util.js');
6
7 js> Function.prototype.bind
8 function bind() { [native code for Function.bind, arity=1] }
9
10 js> expectTypeError(function() {
11 > Function.prototype.bind.call({})
12 > });
13
14 js> typeof (function(){}).bind({})
15 function
16
17 js> function Val() {
18 > this.val = 0;
19 > this.change = function(newVal) { this.val = newVal };
20 > this.valueOf = function() { return 1; }
21 > }
22
23 js> var a = new Val(), b = new Val();
24 js> a.change.bind(b)(1)
25 js> [a.val, b.val].toSource()
26 [0, 1]
27
28 js> new (Val.bind({})) instanceof Val
29 true
30
31 js> new Val() instanceof Val.bind({})
32 true
33
34 js> function add(a, b) { return a + b }
35 js> var add1 = add.bind({}, 1)
36 js> add1(2)
37 3
38 js> var add5 = add.bind({}, 5)
39 js> add5(1)
40 6
41 js> add1(3)
42 4
43
44 js> function LazyAdd(a, b) { this.result = function() { return a + b } }
45 js> var LazyAdd1 = LazyAdd.bind({}, 1)
46 js> new LazyAdd1(4).result()
47 5
48
49 js> add1.length
50 1
51
52 js> Object.isExtensible(add1)
53 true
54
55 js> Object.getPrototypeOf(add1) === Function.prototype
56 true
57
58 js> expectTypeError(function() {
59 > var x = add1.caller
60 > })
61 js> expectTypeError(function() {
62 > add1.caller = 1
63 > })
64 js> expectTypeError(function() {
65 > var x = add1.arguments
66 > })
67 js> expectTypeError(function() {
68 > add1.arguments = 1
69 > })