0

This code works in JSfiddle: https://jsfiddle.net/estevancarlos/w8yo3mp4/1/

let ScrollAnim = (function() {
    console.log('ScrollAnim loaded');

    this.init = function() {
        console.log('init loaded');
    };

    this.getScenes = function() {
        console.log('getScenes loaded');
    };

    return this;
})();

ScrollAnim.init();
ScrollAnim.getScenes();

However it does not work within my actual project, http://veve.io/wip2. I get the error:

Uncaught TypeError: Cannot set property 'init' of undefined

I don't know what is causing this differentiation. Suggestions?

1

1 Answer 1

2

First of all this is not a module pattern. You are polluting global context with your functions.

On jsfiddle you are not using strict mode. And your IIFE is being called in window context. You can check your ScrollAnim is actually window. Demo.

console.log(ScrollAnim === window); //true

Your actuall project is using 'use strict'; resulting in this to be undefined. Which in turn causes the error.

This is module pattern;

let ScrollAnim = (function() {
    console.log('ScrollAnim loaded');

    return {
      init: function() {
        console.log('init loaded');
      },

      getScenes: function() {
        console.log('getScenes loaded');
      }
    };
})();
Sign up to request clarification or add additional context in comments.

Comments

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.