|
| 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 | + |
| 15 | +class Base { |
| 16 | + constructor () { |
| 17 | + this.parent_value = 100; |
| 18 | + } |
| 19 | + |
| 20 | + parent_value () { |
| 21 | + return this.parent_value; |
| 22 | + } |
| 23 | + |
| 24 | + parent_value_arg (a, b, c) { |
| 25 | + if (c) { |
| 26 | + return this.parent_value + a + b + c; |
| 27 | + } else if (b) { |
| 28 | + return this.parent_value + a + b; |
| 29 | + } else { |
| 30 | + return this.parent_value + a; |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + method () { |
| 35 | + return { |
| 36 | + method: function (a, b, c, d, e) { return -50 + a + b + c + d + e; } |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +class Target extends Base { |
| 42 | + constructor () { |
| 43 | + super (); |
| 44 | + this.parent_value = -10; |
| 45 | + } |
| 46 | + |
| 47 | + parent_value () { |
| 48 | + throw new Error ('(parent_value)'); |
| 49 | + } |
| 50 | + |
| 51 | + parent_value_indirect () { |
| 52 | + return super.parent_value.call (this); |
| 53 | + } |
| 54 | + |
| 55 | + parent_value_indirect_arg (a, b, c) { |
| 56 | + if (c) { |
| 57 | + return super.parent_value_arg.call (this, a, b, c); |
| 58 | + } else if (b) { |
| 59 | + return super.parent_value_arg.call (this, a, b); |
| 60 | + } else { |
| 61 | + return super.parent_value_arg.call (this, a); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + method () { |
| 66 | + throw new Error ("(method)"); |
| 67 | + } |
| 68 | + |
| 69 | + parent_method_dot () { |
| 70 | + return super.method.call (this).method (1, 2, 3, 4, 5) |
| 71 | + } |
| 72 | + |
| 73 | + parent_method_index() { |
| 74 | + return super['method'].call (this)['method'] (1, 2, 3, 4, 5); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | + |
| 79 | +var obj = new Target(); |
| 80 | + |
| 81 | +assert (obj.parent_value_indirect () === -10); |
| 82 | +assert (obj.parent_value_indirect_arg (1) === -9); |
| 83 | +assert (obj.parent_value_indirect_arg (1, 2) === -7); |
| 84 | +assert (obj.parent_value_indirect_arg (1, 2, 3) === -4); |
| 85 | + |
| 86 | +try { |
| 87 | + obj.parent_value (); |
| 88 | + assert (false); |
| 89 | +} catch (ex) { |
| 90 | + /* 'obj.parent_value is a number! */ |
| 91 | + assert (ex instanceof TypeError); |
| 92 | +} |
| 93 | + |
| 94 | +assert (obj.parent_method_dot () === -35); |
| 95 | +assert (obj.parent_method_index () === -35); |
| 96 | + |
| 97 | +try { |
| 98 | + obj.method(); |
| 99 | + assert (false); |
| 100 | +} catch (ex) { |
| 101 | + assert (ex.message === '(method)'); |
| 102 | +} |
| 103 | + |
| 104 | +var demo_object = { |
| 105 | + parent_value: 1000, |
| 106 | + method: function () { |
| 107 | + throw new Error ('Very bad!'); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +assert (obj.parent_value_indirect_arg.call (demo_object, 1) === 1001); |
| 112 | +assert (obj.parent_value_indirect_arg.call (demo_object, 1, 2) === 1003); |
| 113 | + |
| 114 | +assert (obj.parent_method_dot.call (demo_object) === -35); |
0 commit comments