Subj : Re: JS_GetFunctionId for an anyomous function To : Mike Moening From : Brendan Eich Date : Tue Mar 29 2005 12:59 pm Mike Moening wrote: > When the NewScript hook is fired some of the functions are anonymous and > return NULL. That's right -- those functions have no declared name, they are indeed anonymous. > In the example below, how do I get the names "execute" and "setN" when their > respective NewScript hooks are fired for the functions on lines 14 and 18? You can't generally impute a name to a function just because that anonymous functions happens to be referenced by certain properties. The function could be named by many properties. If you want to give these functions names, declare names for 'em (this makes for redundancy, of course, but it's the way JS1 works -- JS2 will fix this). So: .. . . this.execute = function execute() { /* Line 14 */ this.results = factor(this.n); } } Factorial.prototype.setN = function setN(n) { /* Line 18*/ this.n = n; } This question comes up a lot, and that's the best answer until JS2/ES4 features start to show up, probably some time this summer. /be > Thanks! > > > Here's the script: > ------------------------------ > function Factorial(n) > { > if(n!=null) > this.n=n; > else > this.n=0; > this.results=0; > var self = this; > function factor(i) { > if (i <= 1) > return 1; > return i * factor(i-1) > } > this.execute = function() { /* Line 14 */ > this.results = factor(this.n); > } > } > Factorial.prototype.setN = function(n) { /* Line 18*/ > this.n = n; > } > > var oFact = new Factorial(); //Create our "object" > oFact.setN(5); //Set the private data. > oFact.execute(); //Call the method. > ---------------------------- > > .