On May 30, 9:35 pm, John Passaniti <put-my-first-name-
h...@JapanIsShinto.comwrote:
Quote:
ron.h.h...@gmail.com wrote:
Quote:
If I understand, then, you wish to have the execution context
available in order to interpolate, but you also wish to do so neither
exclusively within the opening execution context, nor through creation
of a closure in order to re-establish the context.
>
Correct, the ideal is to have what is offered in other languages that
offer variable interpolation built-in to the language. Perl is one example:
>
sub whatever {
my $example = 42;
return "example is: $example";
}
>
That is what I am trying to emulate.
>
Understood from the original description of what you wanted.
Quote:
Quote:
There's a reason that var's within the execution context are referred
to as "private variables". The only access to private variable values
is from within the execution context, or through a method call from
within the execution context that passes the values (or references)
out. Where the latter would seem to fit more closely with Douglas
Crockford's "supplant" method, you've already rejected that as being
"restrictive".
>
I fully understand that yes, private variables are (and should be)
private. And normally, that is exactly what I want in the code I write.
Lexical scoping rules work for me 95% of the time.
>
This is a 5% case. This is a case where I find JavaScript is missing
some functionality I find useful, and I am looking for a way to emulate
it. So I am asking not for approval, but creativity from JavaScript
programmers who are more experienced than I to think of a solution. If
there is none, fine.
You'll see the experts using
setTimeout(function() {..}, 500)
, perhaps not the prettiest of structures, to generate a closure for
use on expiry of the timer.
You sort of had the answer originally when you stated: "And the reason
is clear: JavaScript is evidently a lexically instead of dynamically
scoped language."
There is a form of dynamic scoping of object property reading through
the prototype chain mechanism, as program execution changes to objects
on the prototype chain can affect property resolution.
Nonetheless, for closures, the function to which a reference is held
to maintain a closure on the execution context and related scoping
chains, has the requirement that it be lexically scoped into the
environment in which it is to be utilized.
Quote:
>But I have to believe that there is likely some
solution given the tools JavaScript already provides.
>
Quote:
Seems to me that doesn't leave much room to "find a way that's closer
to what I want", no matter how much you may want it. ;-)
>
Actually, I think I'm coming closer to a solution. I haven't
implemented it yet, so here is a description.
>
My problem has been the explicit creation of a closure to capture the
context. The whole point of this exercise is to reduce the amount of
code I have to write when I want to interpolate variables into strings,
and setting up an explicit closure is just as ugly as streams of
literals and variables being concatenated.
>
While it's easy to see the ideal, surely you don't equate introduction
of a relatively succinct function expression to being "as ugly as
streams of literals and variables being concatenated."
Quote:
It's the creation of an *explicit* closure that I dislike, not the use
of closures at all. So the question now is if there is a way to do this
implicitly.
>
One way may be to change this from a function call to an object
creation. That is, instead of this:
>
var example = 42;
alert(interpolate("example is: {example}"));
>
I think syntax like this may be possible:
>
var example = 42;
alert(new Interpolate("example is: {example}"));
>
Which is conserving in phrasing, albeit misleading and possibly error-
prone to omission of "new", and really not all that far off from:
alert( "example is: {example}".interp( function(_){return
eval(_);}) );
In the latter, nonetheless, one would be inclined to use a function
reference if more than a one-off use was required.
Neither form, however, would come that close to meeting the ideal of a
language-based facility for performing string interpolation. It can be
done, because of Javascript's flexibility, but it's a question of
what's the cost. It appears to be not as unencumbered as one might
hope.
Quote:
The difference is that I would be creating an instance of Interpolate in
the same context. If I can have the constructor build a closure, it may
serve my needs. I'll play with this later and if anything useful comes
out, I'll post it here.
A fine conditional. Success is awaited, in dubiousness ever so. ;)
--
../rh