Last active
July 19, 2018 23:12
-
-
Save manar007/299fb93a69b5d7703366 to your computer and use it in GitHub Desktop.
js bind simple implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function.prototype.bind = function(context){ | |
var that = this, | |
slicedArgs = Array.prototype.splice.call(arguments, 1), | |
bounded = function (){ | |
var newArgs = slicedArgs.concat(Array.prototype.splice.call(arguments)); | |
return that.apply(context,newArgs); | |
} | |
bounded.prototype = that.prototype; | |
return bounded; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@manar007 var newArgs = slicedArgs.concat(Array.prototype.splice.call(arguments)); is always returning empty array. We have to pass another argument inside splice.call like var newArgs = slicedArgs.concat(Array.prototype.splice.call(arguments, 0));