All Downloads are FREE. Search and download functionalities are using the official Maven repository.

rhino1.7.7.testsrc.doctests.function.bind.doctest Maven / Gradle / Ivy

Go to download

Rhino is an open-source implementation of JavaScript written entirely in Java. It is typically embedded into Java applications to provide scripting to end users.

There is a newer version: 1.7.15
Show newest version
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

js> load('testsrc/doctests/util.js');

js> Function.prototype.bind
function bind() { [native code for Function.bind, arity=1] }

js> expectTypeError(function() {
  >   Function.prototype.bind.call({})
  > });

js> typeof (function(){}).bind({})
function

js> function Val() { 
  >   this.val = 0; 
  >   this.change = function(newVal) { this.val = newVal }; 
  >   this.valueOf = function() { return 1; }
  > }

js> var a = new Val(), b = new Val();
js> a.change.bind(b)(1)
js> [a.val, b.val].toSource()
[0, 1]

js> new (Val.bind({})) instanceof Val
true

js> new Val() instanceof Val.bind({})
true

js> function add(a, b) { return a + b }
js> var add1 = add.bind({}, 1)
js> add1(2)
3
js> var add5 = add.bind({}, 5)
js> add5(1)
6
js> add1(3)
4

js> function LazyAdd(a, b) { this.result = function() { return a + b } }
js> var LazyAdd1 = LazyAdd.bind({}, 1)
js> new LazyAdd1(4).result()
5

js> add1.length
1

js> Object.isExtensible(add1)
true

js> Object.getPrototypeOf(add1) === Function.prototype
true

js> expectTypeError(function() {
  >   var x = add1.caller
  > })
js> expectTypeError(function() {
  >   add1.caller = 1
  > })
js> expectTypeError(function() {
  >   var x = add1.arguments
  > })
js> expectTypeError(function() {
  >   add1.arguments = 1
  > })




© 2015 - 2024 Weber Informatics LLC | Privacy Policy