1
(function($) {
    $.fn.my_custom_jquery_plugin = function() {
        var custom_settings = {
            first: 'First',
            second: 'Second'
        }

        function a_custom_function() {
        }
    }
})(jQuery);

I would like to know how I can access the settings variable and a_custom_function function from outside the plugin?

Naif
  • 33
  • 5

1 Answers1

1

You can use the objects data to store your settings:

(function($) {
    $.fn.my_custom_jquery_plugin = function(options) {
        var settings = $.extend({
            first: 'First',
            second: 'Second'
        }, options );
        
        // this points to $('#test') in this example
        this.data("settings", settings)
    }
})(jQuery);

$('#test').my_custom_jquery_plugin({first: 'NotFirst'});
console.log($('#test').data("settings"))
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="test">

Also note that jQuery suggest that you use the minimum possible function names in the global namespace, so if you are just starting your plugin consider using the methods described here.

urban
  • 5,392
  • 3
  • 19
  • 45