Skip to content

Added files d-create-function and e-timeout-callback as home work. #6

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
40 changes: 40 additions & 0 deletions JavaScript/d-create-function.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';

const wrap = (before, after, fn) => {
const args = [];
for (let i = 1; i <= fn.length; i++) {
args.push(`a${i}`);
}
global.fn = fn;
Copy link
Member

Choose a reason for hiding this comment

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

Don't mixin to global

Copy link
Author

Choose a reason for hiding this comment

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

Не знаю как это сделать. До pull request не получалось.

Опирался на информацию с MDN:
_Примечание: функции, созданные конструктором Function, не создают замыканий на их контексты создания; они всегда создаются в глобальной области видимости. При их вызове, они получат доступ только к своим локальным переменным и переменным из глобальной области видимости, но не к переменным в той области видимости, в которой вызывался конструктор Function"

Буду пробовать.

Copy link
Author

Choose a reason for hiding this comment

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

Done

global.before = before;
global.after = after;
const createFn = new Function(...args, `
return after(fn(...before(${args.join(', ')})));
`);
return createFn;
};

// Usage

const func = (par1, par2) => {
console.dir({ par1, par2 });
return [par1, par2];
};

const before = (...args) => {
console.log('before');
return args;
};

const after = res => {
console.log('after');
return res;
};

const wrapped = wrap(before, after, func);
const res = wrapped('Uno', 'Due');
console.dir({
res,
func: func.length,
wrapped: wrapped.length,
});