0

I cope with a coffee script problem when I try to test a javascript object from my it. I have 2 files : a javascript one and a coffee script one which are loaded in this order.

What I need is to define an object in the javascript file:

var my_js_obj = {
    string1: "blablabla",
    string2: "blobloblo",
    string3: "blublublu",
};

And then I try to get this object from my coffee script file :

if not my_js_obj?
  my_js_obj = {}
  console.log "obj does not exist"
else 
  console.log "obj exists"

console.log my_js_obj

In my console, I always get an empty object :

obj does not exist 
Object {}

I don't know if there's an impact but I'm using Ruby on Rails 4.

1 Answer 1

1

my_js_obj is not defined golbally. It's defined as local variable in javascript file. You cannot access it from other file.

If You define it as global variable, then You can access it.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer. I thought the declaration with var outside any function or block was a global definition. Now, it works using window.my_js_obj in both my javascript file and coffee script file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.