Coder Social home page Coder Social logo

I don't get Function.apply about uwebsockets.js HOT 8 CLOSED

 avatar commented on May 3, 2024 1
I don't get Function.apply

from uwebsockets.js.

Comments (8)

frank-dspeed avatar frank-dspeed commented on May 3, 2024
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 !!!!! πŸ’ƒ

from uwebsockets.js.

frank-dspeed avatar frank-dspeed commented on May 3, 2024

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

from uwebsockets.js.

frank-dspeed avatar frank-dspeed commented on May 3, 2024

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

from uwebsockets.js.

 avatar commented on May 3, 2024

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));
}

from uwebsockets.js.

slavaGanzin avatar slavaGanzin commented on May 3, 2024

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

from uwebsockets.js.

 avatar commented on May 3, 2024

Yeah it helped, it works now at least. Thanks

from uwebsockets.js.

slavaGanzin avatar slavaGanzin commented on May 3, 2024

You're welcome. Thank you for UWS

from uwebsockets.js.

frank-dspeed avatar frank-dspeed commented on May 3, 2024

@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)

from uwebsockets.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.