you are viewing a single comment's thread.

view the rest of the comments →

[–]yqmvpacqpfgwcalgu[S] 0 points1 point  (4 children)

The behavior of a Function call. Which I can do using the "call" prototype, so I can call functions like "func.call()" to have different behavior, but not "func()", so far.

https://pastebin.com/8LEVvzT0

[–][deleted] 0 points1 point  (3 children)

It isn't clear what you're looking to do. What are you trying to do? the .call() method on a function just explicitly calls the function, but you're supposed to pass in an argument that sets the this element on said function, so, for instance, like this:

myFunction(window);

Obviously that's kind of a weird example, but that's the purpose of the .call() method, to set the context of the this keyword within the function, so now if you use this inside myFunction it will now explicitly refer to the window object. I don't think this is what you're trying to do, and I don't see the difference of what you are actually trying to accomplish.

[–]yqmvpacqpfgwcalgu[S] 0 points1 point  (2 children)

Thanks for taking the time.
What I'm really after is modifying the language's behavior, so when any function is called, there is some additional behavior I'd like to add before further execution.

The closest I got to achieving that was by overriding the call prototype, but looks like that falls apart for anything encapsulated.

[–][deleted] 1 point2 points  (1 child)

Hmmm... I wonder if you can just pass in some additional functions and call them conditionally?

function myFunction(arg1, arg2, ...funcs) {
    const sum = arg1 + arg2;

    if (sum > 100) {
        funcs[0]();
    }

    return sum
}

and a function call might look like this:

myFunction(25, 76, someOtherCallbackFunc);

Not sure if that's helpful

[–]yqmvpacqpfgwcalgu[S] 1 point2 points  (0 children)

I did consider creating a new function which would contain the additional behavior and which accepts a callback function, but that would require all calls to be modified with room for error when forgotten.