tags:

views:

39

answers:

1

Hello

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

http://stackoverflow.com/questions/124326/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 worked. 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 achive this ?

Thanks in Advance.

Gustavo

+1  A: 

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
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.
Gustavo
@Gustavo: Gotcha. How are you calling the Lingo functions?
T.J. Crowder
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.
Gustavo
@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
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.
Gustavo
@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