From 654135dad00fc56e9ff5f248ea52c66f16b90205 Mon Sep 17 00:00:00 2001 From: Xotic750 Date: Wed, 27 Jan 2016 13:00:11 +0100 Subject: [PATCH] Function#bind robustness. https://github.com/es-shims/es5-shim/issues/378 --- es5-shim.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/es5-shim.js b/es5-shim.js index 9a346c34..0f0498bd 100644 --- a/es5-shim.js +++ b/es5-shim.js @@ -45,7 +45,8 @@ var $Array = Array; var ArrayPrototype = $Array.prototype; var $Object = Object; var ObjectPrototype = $Object.prototype; -var FunctionPrototype = Function.prototype; +var $Function = Function; +var FunctionPrototype = $Function.prototype; var $String = String; var StringPrototype = $String.prototype; var $Number = Number; @@ -55,6 +56,7 @@ var array_splice = ArrayPrototype.splice; var array_push = ArrayPrototype.push; var array_unshift = ArrayPrototype.unshift; var array_concat = ArrayPrototype.concat; +var array_join = ArrayPrototype.join; var call = FunctionPrototype.call; var apply = FunctionPrototype.apply; var max = Math.max; @@ -226,7 +228,8 @@ defineProperties(FunctionPrototype, { // 5. Return the result of calling the [[Construct]] internal // method of target providing args as the arguments. - var result = target.apply( + var result = apply.call( + target, this, array_concat.call(args, array_slice.call(arguments)) ); @@ -255,7 +258,8 @@ defineProperties(FunctionPrototype, { // providing args as the arguments. // equiv: target.call(this, ...boundArgs, ...args) - return target.apply( + return apply.call( + target, that, array_concat.call(args, array_slice.call(arguments)) ); @@ -285,7 +289,7 @@ defineProperties(FunctionPrototype, { // for ex.) all use of eval or Function costructor throws an exception. // However in all of these environments Function.prototype.bind exists // and so this code will never be executed. - bound = Function('binder', 'return function (' + boundArgs.join(',') + '){ return binder.apply(this, arguments); }')(binder); + bound = $Function('binder', 'return function (' + array_join.call(boundArgs, ',') + '){ return binder.apply(this, arguments); }')(binder); if (target.prototype) { Empty.prototype = target.prototype;