Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task bind native impl #34

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions implementations/bind.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/bind

/*
Bind function returns a new bounded function and passes the "this" reference to be used by the targeted function
The bind() function creates a new bound function, which is an exotic function object
that wraps the original function object.
*/

Function.prototype.altBind = function (someObj, ...outerFuncArguments) {
const targetFunc = this;

// return inner function
return function (...innerFuncArgs) {

let propKey = Math.random().toString();
while (someObj.hasOwnProperty(propKey)) {
propKey = Math.random().toString();
}
someObj[propKey] = targetFunc;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use this line of code?
someObj.__proto__.propKey = targetFunc;


// prepend outer args to inner args
const result = someObj[propKey](...outerFuncArguments, ...innerFuncArgs);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use this line of code?
const result = someObj.propKey(...outerFuncArguments, ...innerFuncArgs);

delete someObj[propKey];
Copy link
Member

@TheSTL TheSTL Oct 12, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete someObj.__proto__.propKey;

return result;
};
};