Created
October 19, 2016 08:54
-
-
Save jacobbubu/1fda3e6adec6dfeeba72a773a9ca0cf3 to your computer and use it in GitHub Desktop.
Object.assign
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
assign = (target, firstSource) -> | |
if !target? | |
throw new TypeError('Cannot convert first argument to object') | |
to = Object(target) | |
hasPendingException = false | |
pendingException = undefined | |
i = 1 | |
while i < arguments.length | |
nextSource = arguments[i] | |
if !nextSource? | |
i++ | |
continue | |
keysArray = Object.keys Object(nextSource) | |
nextIndex = 0 | |
len = keysArray.length | |
while nextIndex < len | |
nextKey = keysArray[nextIndex] | |
try | |
desc = Object.getOwnPropertyDescriptor(nextSource, nextKey) | |
if desc != undefined and desc.enumerable | |
to[nextKey] = nextSource[nextKey] | |
catch e | |
if !hasPendingException | |
hasPendingException = true | |
pendingException = e | |
nextIndex++ | |
throw pendingException if hasPendingException | |
i++ | |
to |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment