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

I don't get Function.apply #12

Closed
ghost opened this issue Jan 14, 2019 · 8 comments
Closed

I don't get Function.apply #12

ghost opened this issue Jan 14, 2019 · 8 comments

Comments

@ghost
Copy link

ghost commented Jan 14, 2019

How can I shorten this;

		process.nextTick = (f, ...args) => {
			if (args.length == 1) {
				uWS.nextTick(() => {f(args[0]);});
			}

			if (args.length == 2) {
				uWS.nextTick(() => {f(args[0], args[1]);});
			}

			if (args.length == 3) {
				uWS.nextTick(() => {f(args[0], args[1], args[2]);});
			}

			if (args.length == 4) {
				uWS.nextTick(() => {f(args[0], args[1], args[2], args[3]);});
			}
		}

So that it works, presumably with apply, bind or any such thing. I can't get it working with apply.

@frank-dspeed
Copy link

frank-dspeed commented Jan 14, 2019

process.nextTick = (f, ...args) => {
  uWS.nextTick(() => f(...args));
}

the speard works also with arrays thats why this gets (arg1,arg2,arg3)
i am glad that i could help i saw your rant about NPM and i want to tell you your right it was the right way to do it i am a big fan of it !!!!! 💃

@frank-dspeed
Copy link

frank-dspeed commented Jan 14, 2019

but you got that already in your code apply would look like

process.nextTick = (f, ...args) => {
  console.log(this instanceof global) //=> true!
  uWS.nextTick(() => f.apply(this, args));
} // this is in this case most time the global object  of nodejs or in case of browser window

function withName() {
  console.log(this instanceof global) //=> true!
  uWS.nextTick(() => f.apply(this, args));
}.bind(this)


function withName() {
  console.log(this instanceof global) //=> false!
  console.log(this instanceof withName) //=> true!
  uWS.nextTick(() => f.apply(this, args));
}

in a JS file directly used bind(this) is eqal to not using bind(this) as arrow function don't have own scope

importent side note () => arrow functions are using the outer scope as this so inside a named function the function is the scope if used directly the global object is the scope

@frank-dspeed
Copy link

cant write all examples but if you point me to a file with some pseudo code i get it working for you with comments

@ghost
Copy link
Author

ghost commented Jan 14, 2019

This is what I had:

process.nextTick = (f, ...args) => uWS.nextTick(f.apply(...args));

Which is obviously wrong, but worked good enough for me not to find an issue until now.

I thought this would solve it:

process.nextTick = (f, ...args) => uWS.nextTick(() => {f.apply(...args);});

But it doesn't work..?

If this works, that's like Golang and simple:

process.nextTick = (f, ...args) => {
  uWS.nextTick(() => f(...args));
}

@slavaGanzin
Copy link

slavaGanzin commented Jan 14, 2019

While the syntax of this function is almost identical to that of call(), the fundamental difference is that call() accepts an argument list, while apply() accepts a single array of arguments.

MDN

Apply signature: (context, args)

So when you call f.apply(context, ars) it's same as:

f.call(context, ...args)

and

f.bind(context)(...args)

But if you don't need context setting (set internal this of function), you can just use splat syntax:

f(...args)

Hope this helps

@ghost
Copy link
Author

ghost commented Jan 14, 2019

Yeah it helped, it works now at least. Thanks

@slavaGanzin
Copy link

You're welcome. Thank you for UWS

@frank-dspeed
Copy link

frank-dspeed commented Jan 14, 2019

@alexhultman the code is wrong apply works directly with arrays

don't use spread 3 dot operator when using apply it expects a array as secund argument

f.apply(context, args)
f.call(context, ...args)

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants