mirror of https://github.com/svaarala/duktape.git
Sami Vaarala
11 years ago
3 changed files with 102 additions and 0 deletions
@ -0,0 +1,36 @@ |
|||
/* |
|||
* Test that bind() can also be used with bind(). |
|||
*/ |
|||
|
|||
/*=== |
|||
foo this: [object global] args: foo bar quux baz |
|||
foo this: mythis args: a b c d |
|||
foo this: mythis args: a b c final |
|||
===*/ |
|||
|
|||
function foo(x,y,z,w) { |
|||
print('foo', 'this:', this, 'args:', x, y, z, w); |
|||
} |
|||
|
|||
function test() { |
|||
var f, g, h; |
|||
|
|||
// plain call
|
|||
foo('foo', 'bar', 'quux', 'baz'); |
|||
|
|||
// bound call
|
|||
f = foo.bind('mythis', 'a', 'b', 'c', 'd'); |
|||
f(); |
|||
|
|||
// bind bind() so that when the result is called, the resulting
|
|||
// bind call arguments look like the "f = foo.bind" above
|
|||
g = Function.prototype.bind.bind(foo, 'mythis', 'a', 'b', 'c'); // leave last arg unbound
|
|||
h = g('final'); // give last arg
|
|||
h(); |
|||
} |
|||
|
|||
try { |
|||
test(); |
|||
} catch (e) { |
|||
print(e); |
|||
} |
@ -0,0 +1,32 @@ |
|||
/* |
|||
* Use bind() indirectly with call() or apply(). |
|||
*/ |
|||
|
|||
/*=== |
|||
apply |
|||
foo this: boundthis args: argx argy argz undefined |
|||
call |
|||
foo this: boundthis args: argx argy argz undefined |
|||
===*/ |
|||
|
|||
function foo(x,y,z,w) { |
|||
print('foo', 'this:', this, 'args:', x, y, z, w); |
|||
} |
|||
|
|||
function test() { |
|||
var f1, f2; |
|||
|
|||
print('apply'); |
|||
f1 = foo.bind.apply(foo, [ 'boundthis', 'argx', 'argy', 'argz' ]); |
|||
f1(); |
|||
|
|||
print('call'); |
|||
f2 = foo.bind.call(foo, 'boundthis', 'argx', 'argy', 'argz'); |
|||
f2(); |
|||
} |
|||
|
|||
try { |
|||
test(); |
|||
} catch (e) { |
|||
print(e); |
|||
} |
@ -0,0 +1,34 @@ |
|||
/* |
|||
* Use eval() indirectly with call() or apply(). |
|||
*/ |
|||
|
|||
/*=== |
|||
apply |
|||
hello from eval! |
|||
call |
|||
hello from eval! |
|||
bind |
|||
hello from eval! |
|||
===*/ |
|||
|
|||
function test() { |
|||
var code = 'print("hello from eval!");'; |
|||
var f; |
|||
|
|||
print('apply'); |
|||
eval.apply(this, [ code ]); |
|||
|
|||
print('call'); |
|||
eval.call(this, code); |
|||
|
|||
/* Also test indirect use of eval through bind() */ |
|||
print('bind'); |
|||
f = eval.bind(this, code); |
|||
f(); |
|||
} |
|||
|
|||
try { |
|||
test(); |
|||
} catch (e) { |
|||
print(e); |
|||
} |
Loading…
Reference in new issue