0

I have multiple functions within plugin that I like to call through out but for some reasons I get methods undefined.

If I don't use methods to wrap the functions I get

function statement requires a name

  1. What am I doing wrong here?
  2. do I need to wrap the functions with methods var?

    (function ($) {
    
    var methods = {
    
        // GET TARGET LOCATION AND ANIMATE
        scrollTopPage: function () {
    
            $(htmlBody).animate({
                scrollTop: 0
            }, 'slow');
    
        },
    
        scrollToSect: function (htmlBody) {
    
            $(htmlBody).animate({
    
                scrollTop: $('#page-id').offset().top
            });
    
        },
    
        goToID: function (sectID) {
    
            var htmlBody = 'html,body';
    
            //Scroll to the very top
            if (sectID == 'home') {
    
                methods.scrollTopPage(htmlBody);
    
            } else {
    
    
                methods.scrollToSect(htmlBody);
    
            }
    
        }
    
    } // End the Methods/Functions
    
    
    $.fn.pageScroller = function (methods) {
    
    
        this.click(function (e) {
    
            sectID = $(this).attr('id');
    
                e.preventDefault();
    
                // Undefined Error
                methods.goToID(sectID); // Call the goToID function and pass the sectID variable
    
                    $(this).parent().addClass('current').prev().removeClass('current');
                    $(this).parent().addClass('current').next().removeClass('current');
    
    
        });
    
        $(document).keydown(function (e) {
    
            //To Do Re Use the methods/functions here
    
        });
    
    };
    
    })(jQuery);
    

2 Answers 2

1

You are assigning a variable, not calling the function at:

 $.fn.pageScroller = function (methods) { ....

I guess that this results in when the function is actually called from the $.fn.pageScrollerplugin, or whatever calls this, the parameter will be named methods, but will not be the methods object you created. Instead, it will be whatever the invoker chooses to pass as parameter. In your case, it seems as no parameter is passed at all. That's why methods is undefined.

If you don't set a parameter for this function, does it not lead to that methods will reference your collection of functions instead of a parameter passed?

Ie.: try this, and hope methods is still accessible:

 $.fn.pageScroller = function () { ....
Sign up to request clarification or add additional context in comments.

Comments

0

one other way is having methods property inside plugin like this...

(function ($) {

$.fn.pageScroller = function () {

  this.methods = {

    // GET TARGET LOCATION AND ANIMATE
    scrollTopPage: function () {

        $(htmlBody).animate({
            scrollTop: 0
        }, 'slow');

    },

    scrollToSect: function (htmlBody) {

        $(htmlBody).animate({

            scrollTop: $('#page-id').offset().top
        });

    },

    goToID: function (sectID) {

        var htmlBody = 'html,body';

        //Scroll to the very top
        if (sectID == 'home') {

            methods.scrollTopPage(htmlBody);

        } else {


            methods.scrollToSect(htmlBody);

        }

    }

} // End the Methods/Functions



    this.click(function (e) {

        sectID = $(this).attr('id');

            e.preventDefault();

            // Undefined Error
            methods.goToID(sectID); // Call the goToID function and pass the sectID variable

                $(this).parent().addClass('current').prev().removeClass('current');
                $(this).parent().addClass('current').next().removeClass('current');


    });

    $(document).keydown(function (e) {

        //To Do Re Use the methods/functions here

    });

};

})(jQuery);

or also you can also pass methods while creating plugin...

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.