Stephan Tolksdorf writes:
 > Writing a large amount of code with document.write has always been a
 > problem for Navigator.

Accumulating text in a variable might be better than calling
document.write many times (certainly shorter), but don't do it with a
series of separate assignments like this:

var html = '';
html += 'one ';   // create string 'one '
html += 'two ';   // create string 'one two '
html += 'three '; // create string 'one two three '
...

Doing that creates lots of intermediate garbage strings that adds more
work for garbage collection, and it takes more time as the strings get
longer and longer.  Instead do a few larger assignments using
concatenation:

var html = '';
html += 'one ' + 
        'two ' + 
        'three ';

 > Executing the code with eval could be an alternative, but then every
 > declaration would have to be rewritten into an assignment (e.g.
 > function f() {} => f = function () {}). Doesn't seem to be an
 > option...

I didn't follow why there is a need for eval, but changing to use of
anonymous function literals (e.g. f = function () {}) does NOT use eval
when the function is called; it merely makes the function anonymous in
that the function itself doesn't know its name.  You may be thinking of
the Function constructor (e.g. f = new Function('', '')) which creates a
function given strings for the args and body.  I remember reading
previously, though I can't find it now, that such functions use eval to
reevaluate the strings each time the function is called.

The context that eval uses to evaluate a string is never clear to me -
documentation rarely mentions this crucial bit of info, and it could be
either the local context or the global context, so I keep forgetting
which it is.  It really needs a way of specifying the context, which is
what you were asking for.  The Object object used to have an eval
function that was applied to the object (e.g. someObj.eval('some
string')), but that has been deprecated I believe.  Using eval is bad
anyway for several reasons, so it is best to avoid it and you almost
always can.

 > Or does someone know a way to declare a global function within a local
 > block or a way to change the execution context of an eval statement?

You can use a function anywhere no matter where it was declared and the
function behaves as if it were in the context it was declared in.  This
is true for both named and anonymous functions, but not those new
Function() objects (assuming they are reevaluated each time they are
called).

To use an anonymous function literal in a global context but as if it is
inside some local context, define it in the local block and assign it to
a global variable, or pass it out as the result of the local block.
Then in the global context, call the function.

var b = 1;
var global_f;

function set_global_f (a) {
  global_f = function (x) { return a + b + x };
}

set_global_f(3);

global_f(5) => 9

---------------OR---------------

var b = 1;
var global_f;

function make_f (a) {
  return function (x) { return a + b + x }
}

global_f = make_f(3);
global_f(5) => 9


------

Even a named function defined in a local context should be callable
outside of the local context, if you pass the function object out either
by assigning it to a variable or returning it as a result.  I'm not sure
whether the function itself remembers its name (debuggers know), but in
any case that local name should not be usable outside of the local
context.  Something to test...

Hope that helps.

-- 
Daniel LaLiberte
[EMAIL PROTECTED]
http://www.HoloNexus.org/~liberte/
http://www.HyperNews.org/~liberte/

_______________________________________________
Dynapi-Dev mailing list
[EMAIL PROTECTED]
http://www.mail-archive.com/dynapi-dev@lists.sourceforge.net/

Reply via email to