tizen beta release
[framework/web/webkit-efl.git] / LayoutTests / fast / js / script-tests / function-bind.js
1 description("Tests Function.bind.");
2
3 var result;
4
5 function F(x, y)
6 {
7     result = this + " -> x:" + x + ", y:" + y;
8 }
9
10 G = F.bind("'a'", "'b'");
11 H = G.bind("'Cannot rebind this!'", "'c'");
12
13 F(1,2);
14 shouldBe("result", '"[object DOMWindow] -> x:1, y:2"');
15 G(1,2);
16 shouldBe("result", '"\'a\' -> x:\'b\', y:1"');
17 H(1,2);
18 shouldBe("result", '"\'a\' -> x:\'b\', y:\'c\'"');
19
20 var f = new F(1,2);
21 shouldBe("result", '"[object Object] -> x:1, y:2"');
22 var g = new G(1,2);
23 shouldBe("result", '"[object Object] -> x:\'b\', y:1"');
24 var h = new H(1,2);
25 shouldBe("result", '"[object Object] -> x:\'b\', y:\'c\'"');
26
27 // Objects f & g & h are all actually instances of F, G's HasInstance method
28 // actually is checking whether the argument is an instance of F, and H's
29 // HasInstance calls G's which calls F's!
30 shouldBeTrue('f instanceof F');
31 shouldBeTrue('f instanceof G');
32 shouldBeTrue('f instanceof H');
33 shouldBeTrue('g instanceof F');
34 shouldBeTrue('g instanceof G');
35 shouldBeTrue('g instanceof H');
36 shouldBeTrue('h instanceof F');
37 shouldBeTrue('h instanceof G');
38 shouldBeTrue('h instanceof H');
39
40 // Bound functions don't have a 'prototype' property.
41 shouldBeTrue('"prototype" in F');
42 shouldBeFalse('"prototype" in G');
43 shouldBeFalse('"prototype" in H');
44
45 // The object passed to bind as 'this' must be callable.
46 shouldThrow('Function.bind.call(undefined)');
47 // Objects that allow call but not construct can be bound, but should throw if used with new.
48 var abcAt = String.prototype.charAt.bind("abc");
49 shouldBe('abcAt(1)', '"b"');
50 shouldThrow('new abcAt(1)');
51
52 // This exposes a bug in our implementation of instanceof. The prototype property should not
53 // be accessed until after we have established the default HasInstance is being executed.
54 // https://bugs.webkit.org/show_bug.cgi?id=68656
55 var boundFunctionPrototypeAccessed = false;
56 P = F.bind(1,2);
57 Object.defineProperty(P, 'prototype', { get:function(){ boundFunctionPrototypeAccessed = true; } });
58 f instanceof P;
59 shouldBeFalse('boundFunctionPrototypeAccessed');
60
61 shouldBe('Function.bind.length', '1');