• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
15try {
16  Reflect.construct ();
17  assert (false);
18} catch (e) {
19  assert (e instanceof TypeError);
20}
21
22try {
23  Reflect.construct (Date);
24  assert (false);
25} catch (e) {
26  assert (e instanceof TypeError);
27}
28
29var d = Reflect.construct (Date, [1776, 6, 4]);
30assert (d instanceof Date);
31assert (d.getFullYear () === 1776);
32
33function func1 (a, b, c) {
34  this.sum = a + b + c;
35}
36
37var args = [1, 2, 3];
38var object1 = new func1 (...args);
39var object2 = Reflect.construct (func1, args);
40
41assert (object2.sum === 6);
42assert (object1.sum === 6);
43
44function CatClass () {
45  this.name = 'Cat';
46}
47
48function DogClass () {
49  this.name = 'Dog';
50}
51
52var obj1 = Reflect.construct (CatClass, args, DogClass);
53assert (obj1.name === 'Cat');
54assert (!(obj1 instanceof CatClass));
55assert (obj1 instanceof DogClass);
56
57try {
58  Reflect.construct (func1, 5, 5);
59  assert (false);
60} catch (e) {
61  assert (e instanceof TypeError);
62}
63
64try {
65  Reflect.construct (5, 5);
66  assert (false);
67} catch (e) {
68  assert (e instanceof TypeError);
69}
70
71function func2 () {
72  throw 5;
73}
74
75try {
76  Reflect.construct (func2, {});
77  assert (false);
78} catch (e) {
79  assert (e === 5);
80}
81