0

I am attempting to use namespaces to better organize my sites very complicated code. I have:

var myApp = {};

myApp.fp = {
    brandCarousel: null,

    init: function() {
        // initialize brand carousel
        this.brandCarousel = new Pluit.Carousel('brand-scroll-outer', {  circular: false  });
    }
};

document.observe("dom:loaded", function() { myApp.fp.init();  });

I am using prototype 1.7 and the Pluit Carousel library.

If I try to call one of Pluit Carousels methods (moveNext) by typing:

myApp.fp.brandCarousel.moveNext()

I get a JavaScript error:

TypeError: Object # has no method 'moveNext'

Is there something I am getting wrong when it comes to scope or namespacing? How can I access this internal function?

1 Answer 1

0

Nothing about your particular example looks especially wrong (except that it switches between myApp and masApp (typo?)). Here's a stripped-down, dummy example to show that your approach works (and that your problem must be in another location):

var myApp = {};

myApp.fp = {
    brandCarousel: null,

    init: function() {
        // initialize brand carousel
        this.brandCarousel = new (function() {
            this.moveNext = function() {
                alert("moving!");
            };
        })  
    }
};

myApp.fp.init()
myApp.fp.brandCarousel.moveNext()

The above code alerts moving! as expected.

1
  • Hmm... I think it must have to do with how the Pluit carousel is set up. Was able to hack the script a bit to get it to do what I needed. Thanks!
    – Rebecca
    Nov 14, 2011 at 22:47

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.