2

I'm having this problem in a real-world Project.

How to convert an "object" into a function in JavaScript?

I need at least as Steve said "to assign a method operator() to an object". Re-assign is not an option, because the object is too complex and it's not created within Javascript.

I have an object x (but not a function). And I want to call a method this way

x()

I tried to add an apply and a call method but it didn't work. It seems that Javascript remember always that "is not a function" (that's the error I obtain always). Is there a way to "hack" the Spidermonkey engine to achieve this ?

Community
  • 1
  • 1
Gus
  • 25,839
  • 2
  • 51
  • 76

1 Answers1

3

Well, as people told you in that question, you can't retroactively make a function out of something that isn't a function. And if you could (in JavaScript), I'd be very leery of trying it with an object that is "...not created within JavaScript."

What is the real, end goal? Because I suspect there's a better way to get there.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • The end goal is avoid adapter/wrappers in the Javascript code. I'm working in Director with both Lingo and Javascript languages and I have an object that represents functions in Lingo. I want to be able to call those functions from Javascript as any other JS function value. At the moment I'm wrapping and it works fine, but I have to do it explicity. That's the problem. – Gus Feb 06 '10 at 13:14
  • @Gustavo: Gotcha. How are you calling the Lingo functions? – T.J. Crowder Feb 06 '10 at 13:37
  • From Javascript I can call any method declared in the Lingo Object. At the moment I've defined in lingo an apply method that respect the signature of Javascript Function's apply. This is my wrapper Function in Javascript: function WrapFunctionValue(fn) { if (typeof(fn)=="function") return fn; return function() { return fn.apply(0,arguments); }; } As I said before it works but I would like to call directly: fn(args) without caring about wrapping. – Gus Feb 06 '10 at 13:53
  • @Gustavo: I think the answer to my question is buried in there somewhere. You're saying if you have a Lingo object `x`, you can call it via `x.apply(...)`? That's an interesting (and confusing) choice of method name they've used there. I don't think you have a choice about wrapping. – T.J. Crowder Feb 06 '10 at 14:08
  • Thanks, probably I was not clear enough. It is not a real Lingo function, it's a Lingo object. I know in Javascript all functions are objects, but in Lingo is not so. In fact Lingo has no functions, it has Handlers that acts as functions. I did an object myself, a kind of command object pattern. I can't have a method in Lingo to invoke an object this way: x() Now going back to my original question: can I do it with Javascript? Maybe the answer it's in Spidermonkey's source code which I'm going to read now. – Gus Feb 06 '10 at 17:02
  • @Gustavo: No, you can't retroactively make an object -- especially not a foreign, non-JavaScript object -- a function. Wrapping is your best bet (and not that costly). – T.J. Crowder Feb 06 '10 at 17:06