0

I use Coffeescript in my gulpfile and I would like to use an object declared in the same array but it doesn't work as I can see…

# SOURCES
src =
    libs: [
        "#{dir.libs}/jquery/dist/jquery.min.js"
        "#{dir.libs}/gsap/src/uncompressed/TimelineLite.js"
        "#{dir.libs}/gsap/src/uncompressed/easing/EasePack.js"
        "#{dir.libs}/page.js/page.js"
        "#{dir.libs}/swiper/build/js/swiper.js"
    ]
    js: [
        "#{dir.js}/site.js"
        "#{dir.js}/functions.js"
        "#{dir.js}/animations.js"
        "#{dir.js}/init.js"
    ]
    javascript: src.libs + src.js # this.libs + this.js is the same

It's returning NaN
Made it wrong? 🙈

1 Answer 1

1

When you say:

javascript: src.libs + src.js

your src is still undefined because you're in the process of building it. You also seem to be under the mistaken assumption that + concatenates arrays in (Coffee|Java)Script but it doesn't, array + array will stringify the arrays and then concatenate the strings so:

[ 'a' ] + [ 'b' ]

is actually 'ab'.

To solve the first problem, build src in two steps. To solve the second, use Array::concat:

src =
    libs: [
        "#{dir.libs}/jquery/dist/jquery.min.js"
        "#{dir.libs}/gsap/src/uncompressed/TimelineLite.js"
        "#{dir.libs}/gsap/src/uncompressed/easing/EasePack.js"
        "#{dir.libs}/page.js/page.js"
        "#{dir.libs}/swiper/build/js/swiper.js"
    ]
    js: [
        "#{dir.js}/site.js"
        "#{dir.js}/functions.js"
        "#{dir.js}/animations.js"
        "#{dir.js}/init.js"
    ]
src.javascript = src.libs.concat(src.js)

Or you could define libs and js first if that makes more sense:

libs = [
    "#{dir.libs}/jquery/dist/jquery.min.js"
    "#{dir.libs}/gsap/src/uncompressed/TimelineLite.js"
    "#{dir.libs}/gsap/src/uncompressed/easing/EasePack.js"
    "#{dir.libs}/page.js/page.js"
    "#{dir.libs}/swiper/build/js/swiper.js"
]
js = [
    "#{dir.js}/site.js"
    "#{dir.js}/functions.js"
    "#{dir.js}/animations.js"
    "#{dir.js}/init.js"
]
src =
    libs: libs
    js: js
    javascript: libs.concat(js)
Sign up to request clarification or add additional context in comments.

1 Comment

Ha! didn't know about that. Prefer the first option for declaring my javascript array. Thank you!

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.