0

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

halfbit
  • 3,773
  • 2
  • 34
  • 47
  • Sorry but I don't understand what you're trying to do. You `new GAIA` but purposely return a function and expect it to be something else? You want `gaia` to be both a function and a `GAIA`? – mu is too short Dec 03 '13 at 21:14
  • @muistooshort: my new Gaia (later version) returns a function, yes, i like that. I want to have it not "forgotten" the other _proto_ informations ( as if i get a gaia (new GAIA) not beeing/returning a function) - or prob there are only too many knots in my brain :-( – halfbit Dec 03 '13 at 21:25
  • @muistooshort: a function is an object, true? why cant I get a function like any other object, with predefined proberties? – halfbit Dec 03 '13 at 21:26
  • 1
    A function with properties is easy (http://jsfiddle.net/ambiguous/qm4yE/). So what's the problem? – mu is too short Dec 03 '13 at 21:49
  • I gtg - I think of your example, very nice - vote up your interest, stil missing the .prototype. here (look at my edit aboce, if you still want) – halfbit Dec 03 '13 at 21:59
  • So you're trying to get `gaia instanceof Function` and `gaia instanceof Gaia` both to be true? – mu is too short Dec 03 '13 at 22:21
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/42443/discussion-between-halfbit-and-mu-is-too-short) – halfbit Dec 03 '13 at 23:17
  • If you return an object from a constructor function that is anything other than `this` you are not creating an instance of GAIA. Therefore the returned object does not use the prototype of GAIA constructor for member lookup in the prototype chain. More information about prototype can be found here: http://stackoverflow.com/a/16063711/1641941 – HMR Dec 03 '13 at 23:52
  • so my " ... I extend the constructor function with a return (probably this is the problem)" is the problem, I will walk through the link(s) you gave. – halfbit Dec 04 '13 at 00:09

1 Answers1

0

One thing you could do is return a closure, all the properties you want to define on the returned function you can define before returning the function. I would not advice using this to create many instances because it's less effective as using constructor functions but this will return a function that has a modules member. The modules member is unique to every instance and not shared as it would be on pototype:

@GAIA= (world_size) ->
   console.log "here is a new gaia with" + world_size
   ret =  (continent) ->
       console.log "I stay on "+continent+ " " + me.modules
       console.log(world_size)
   me=ret
   ret.modules=new Array
   ret

g = GAIA("small")
g.modules.push "hello"
g("Asia")
HMR
  • 37,593
  • 24
  • 91
  • 160