I’m trying to access to a class variable through an instance method through an eval (Function)
class Foo
@classVariable = "helow"
class Bar extends Foo
bar: -> (new Function("console.log(Foo.classVariable)")).call @
baz: -> console.log(Foo.classVariable)
(new Bar()).baz()
(new Bar()).bar()
but method bar raise an error, telling me ReferenceError: Foo is not defined
Any advices ?
Is there another to access a class variable ?
When you create a function by passing a string to the
Functionconstructor, that function can only see the global scope (see the MDN docs). If you wrotethen your code would work. Alternatively, instead of using the
Functionconstructor, just useeval: