dimanche 7 décembre 2014

coffeescript scope of variable assignment vs property assignment in object's other properties


Vote count:

0




I'm writing some widgets for Ubersicht. It uses a node.js server and treats each .coffee file as a standalone widget object. I'm having issues defining constant settings to be used throughout one file. Currently I know of two ways to define this type of constant at the top of the file.



# Way 1
foo_1 = true
bar_1 = false
# Way 2
foo_2: true
bar_2: false


Further down in the same file either a property is assigned as a string or as a function. Each of the above two ways of defining an option only works in one of the two property types.



staticProperty: """Output #{foo_1} works here
but output of #{foo_2} doesn't work
"""

methodProperty: (input) ->
if foo_1 # Raises foo_1 is not defined
if @foo_1 # foo_1 is undefined which is expected
if @foo_2 # This works fine


I understand that way 2 add to the object's properties, but I'm not too sure how the way 1 assignment works given that the file is essentially defining an object. Can you explain this?


Also is there a way to define a variable that can be accessed from both places?



asked 3 mins ago







coffeescript scope of variable assignment vs property assignment in object's other properties

Aucun commentaire:

Enregistrer un commentaire