Rigial9
Rigial9

Reputation: 21

Javascript function calling syntax

(function($,window,undefined) {

    $.fn.simplyScroll = function(options) {
        return this.each(function() {
            new $.simplyScroll(this,options);
        });
    };

    var defaults = {
        customClass: 'simply-scroll',
        frameRate: 30, //No of movements per second
        speed: 2, //No of pixels per frame
        orientation: 'horizontal', //'horizontal or 'vertical' - not to be confused with device orientation
        auto: true,
        autoMode: 'loop', //auto = true, 'loop' or 'bounce',
        manualMode: 'end', //auto = false, 'loop' or 'end'
        direction: 'forwards', //'forwards' or 'backwards'.
        pauseOnHover: false, //autoMode = loop|bounce only
        pauseOnTouch: true, //" touch device only
        pauseButton: false, //" generates an extra element to allow manual pausing 
        startOnLoad: false //use this to delay starting of plugin until all page assets have loaded
    };

Hi I'm pretty new to javascript and the syntax, especially function wise. How would I get the value of auto in the defaults object when it's wrapped in a function like this?

I know to get auto would just be defaults.auto, but since the function isn't named I'm not sure how to even get into it. I'm planning on changing the value at a later point in an external javascript file.

Upvotes: 0

Views: 118

Answers (2)

frogatto
frogatto

Reputation: 29285

In my opinion, there are 2 ways that allows you to access to defaults object outside this function scope

1. In function scope, defaults object will be assigned to a propery of global object (= window)

...
// In the function scope
window.defobject = defaults;
...

For example:

(function($, window, undefined) {
    var defaults = {
        foo : "bar"
    };

    window.defobject = defaults;
})(jQuery, this);

console.print(defobject); // This is valid with no error

2. The function finally return this object as output

...
// In the function scope
return defaults;

For example:

var defobject = (function($, window, undefined) {
    var defaults = {
        foo : "bar"
    };

    return defaults;
})(jQuery, this);

console.print(defobject); // This is also valid with no error

Upvotes: 0

cdhowie
cdhowie

Reputation: 169291

You cannot obtain this value if your code is outside of the function, unless this function stores the object in the defaults variable somewhere where it can be reached through a global variable. This is due to the way that JavaScript functions work -- code external to the function has no access to that function's local variables. (This is not very different from other programming languages.)

If your goal is only to change the value rather than obtain the default value, most jQuery plugins will allow you to pass in an object whose properties will override the default settings. Based on the name of the plugin, you could do so like this:

$("#something").simplyScroll({
    auto: false
});

Upvotes: 1

Related Questions