[Laszlo-dev] Declaring fields in a singleton class.
P T Withington
ptw at openlaszlo.org
Tue Feb 13 19:19:08 PST 2007
var in a method is local, not an instance slot.
Max's 'js way' to declare a singleton:
Singleton = {
foo: 32 // instance var
doBar: function (...) { ... } // method
}
We need a js2 way for singletons. Max suggested:
static class Singleton {
var foo = 32;
function bar(...) { ... };
}
But Jim couldn't find a precedent for 'static class' meaning singleton.
We're open to suggestions.
In Dylan, a singleton is created by over-riding make to always return
the same instance. We could do that:
class Singleton {
static var unique;
function make() {
return unique || unique = super();
}
}
[That's a sketch, don't try it at home.]
On 2007-02-13, at 19:51EST, Phillip George Apley wrote:
> In a singleton class such as LzFontManager=new Object; a field such
> as LzFontManager.fonts may be declared
> within a method. In this case LzFontManager.__findMatchingFont uses
> var fonts=... to declare the field.
> Is there a way to declare such a variable outside a method (for
> documentation purposes)?
>
> I tried this:
>
> LzFontManager = new Object;
> var LzFontManager.fonts;
>
> and was surprised to get
>
> Syntax error: the token "LzFontMgr" was not expected at this position.
>
> What I was originally trying to confirm was that the scope of 'var
> fonts' within
> the method was not local to the method definition.
>
> Here's the actual test script I used:
> <canvas width="100%" height="80%" debug="true">
> <debug fontsize="12"/>
>
> <script>
>
> var LzFontMgr = new Object;
> var LzFontMgr.fonts;
>
> LzFontMgr.__findMatchingFont = function () {
> var fonts = "defined internal to a function";
> debug.write(fonts);
> }
>
> LzFontMgr.fonts = "defined external to the function";
> LzFontMgr.__findMatchingFont();
> debug.write(LzFontMgr.fonts);
>
> </script>
> </canvas>
More information about the Laszlo-dev
mailing list