Files
sdk/tests/lib_2/js/method_call_on_object_test.dart
T
Leaf Petersen b101a7d002 Add language versions to _2 test libraries
Change-Id: Ib33169c3e0ffc870915c189404074a1dea472546
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/196548
Reviewed-by: Bob Nystrom <rnystrom@google.com>
Commit-Queue: Leaf Petersen <leafp@google.com>
2021-04-26 17:58:57 +00:00

68 lines
1.4 KiB
Dart

// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// @dart = 2.9
// Tests method calls (typed and dynamic) on various forms of JS objects.
@JS()
library js_parameters_test;
import 'package:js/js.dart';
import 'package:expect/expect.dart';
@JS()
external void eval(String code);
@JS()
class Foo {
external Foo();
external dynamic method(int x);
}
@JS()
external Foo makeFooLiteral();
@JS()
external Foo makeFooObjectCreate();
main() {
// These examples from based on benchmarks-internal/js
eval(r'''
self.Foo = function Foo() {}
self.Foo.prototype.method = function(x) { return x + 1; }
self.makeFooLiteral = function() {
return {
method: function(x) { return x + 1; }
}
}
// Objects created in this way have no prototype.
self.makeFooObjectCreate = function() {
var o = Object.create(null);
o.method = function(x) { return x + 1; }
return o;
}
''');
var foo = Foo();
Expect.equals(2, foo.method(1));
foo = makeFooLiteral();
Expect.equals(2, foo.method(1));
foo = makeFooObjectCreate();
Expect.equals(2, foo.method(1));
dynamic dynamicFoo = Foo();
Expect.equals(2, dynamicFoo.method(1));
dynamicFoo = makeFooLiteral();
Expect.equals(2, dynamicFoo.method(1));
dynamicFoo = makeFooObjectCreate();
Expect.equals(2, dynamicFoo.method(1));
}