tI try to learn some (really) basics in javascript. The following is an abstract problem, but I want to understand it better, what happens with template
and object
in javascript:
What I try to get is a function with predefined properties
Imagine, that I have two lines in each (coffescript) I use;
@GAIA.prototype.modules?=new Array
@GAIA.prototype.modules.push(__FILE__) #lets assume thats the scriptname
this compiles correctly to a global
object.
one script has an additionaly cunstructor function declared:
@GAIA= (world_size) ->
say "here is a new gaia with" + world_size
later on, in a window load
section, I create a new gaia
and say it (console.log)
gaia=new window.GAIA('a small world')
say gaia
what I get is like expected:
here is a new gaia with a small world
Object { modules=[3]}
modules ["file 1", "file 3", "file 2"]
but I want gaia
(or @GAIA) to be a function (again I want to learn), so I extend the constructor function with a return (probably this is the problem)
@GAIA= (world_size) ->
say "here is a new gaia with" + world_size
(continent) ->
say "I stay on "+continent
again in the window load
section but extended:
gaia=new window.GAIA('a small world')
say gaia
say gaia('pangae')
and now I get
here is a new gaia witha small world
function()
I stay on pangae
the debugger still shows me the prototype values like before:
GAIA
function()
prototype
Object { modules=[3]}
modules
["file 1", "file 3", "file 2", ...]
constructor
function()
but in the new created gaia, these are gone.
So the point is:
- getting a 'gaia' with properties - as object
or
- getting a 'gaia' w/o properties - as function
But I want it all(!) :-)
What do I wrong?
(I am not asking for workaround)
edit
after:
gaia=new window.GAIA('a small world')
# move prototype 'manualy' ...
for p,v of window.GAIA.prototype
gaia[p]=v
I get what I want, but i dont think that this is valid / correct