Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript jQuery plugin help

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #173190
    Anonymous
    Inactive

    I’m working on a a jQuery plugin and basicary i want to know the way to use true or false when applying options. For example

            var defaults = $.extend ({
                    valueForCSS:  "red",
                trueOrFalse: true
            }, options);
    

    I know how to use valueForCSS. which is basically like this
    $(this).css(“background”,defaults.valueForCss);

    but how do i give the value for trueOrFalse action? how do i have false mean one thing, and true mean another? This isn’t mentioned in the documentation

    #173209
    noahgelman
    Participant

    Have you ever heard of a “Bool” type variable?

    A Bool type variable is a variable that is equal to only True or False. Here is an example using what you posted in a “Slider” type of plugin

     var defaults = $.extend ({
       slide:  "horizontal",
       thumbnails : true
     }, options);
    

    In the above case, which is hopefully easier to understand, are some default options for a slider plugin. 1st option is “slide” which equals “horizontal”. “slide” could equal lots of things like “horizontal”, “vertical”, “diagonally”, etc. so we store it as a “String” type variable.

    But for thumbnails, we either have them or we don’t, so we use a “Bool” type.

    Later on we can do something like this:

    // ... plugin code
    
    if(thumbnails) {
    
      // ... add thumbnails to slider code
    
    }
    
    // ... more plugin code
    

    If thumbnails is true, the thumbnail code is added, if it is set to false, it isn’t.

    Usually, when you use a Bool variable, you don’t “set” it to something or set something to it. You usually use it to test conditions and change what you do based on those conditions.

    Hopefully that made sense. Let me know if anything needs more explaining.

Viewing 2 posts - 1 through 2 (of 2 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.