Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3975682
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:42:51+00:00 2026-05-20T04:42:51+00:00

I’m working on a jQuery plugin. When the plugin runs, the first thing it

  • 0

I’m working on a jQuery plugin. When the plugin runs, the first thing it does is determine its settings. It does this by taking some default settings and overriding some or all of them with whatever the user passed in.

Here’s a simplified version:

(function( $ ) {
  $.fn.somePlugin = function(userOptions) {
    // Short name for internal use - exposed below for external modification
    var defaults = $.fn.somePlugin.defaults;

    // Decide settings
    // User settings overrule defaults, but merge both into an empty 
    // hash so that original defaults hash isn't modified
    var settings = $.extend(true, {}, defaults, userOptions);

    settings.alerter.call();

    // More code, etc.
  }

  $.fn.somePlugin.defaults = {
    name: 'Captain Slappy von Martinez, Esquire, DDS',
    favoriteColor: 'red',
    alerter: function(){alert("I got bones!");}        
  }
  
})( jQuery );

So, if you call the plugin like this:

    $('div').somePlugin({alerter: function(){alert('No bones here!');}});

… you override the default alerter function, but use the default favoriteColor.

I’m defining the defaults outside the plugin itself so that they are publicly accessible. So if you want to change the default value for favoriteColor, you can just do this:

$.fn.somePlugin.defaults.favoriteColor = 'blue';

… and that will change it for every instance on the page after that.

All of this works fine.

Here’s my problem: in one of the default functions, I need to refer to settings. Something like this:

  $.fn.somePlugin.defaults = {
    name: 'Captain Slappy von Martinez, Esquire, DDS',
    favoriteColor: 'red',
    alerter: function(){alert("Paint me " + settings.favoriteColor);}        
  }

But when I try this, I get a Javascript error on the line where the default alerter function is declared: "settings is not defined." True enough: it’s not defined yet when the parser hits that line. It WILL be defined by the time the function runs, but that doesn’t seem to matter.

So my question is: How can I import my default function into the plugin in such a way that it has access to settings?

  • Is there a way to delay evaluation of settings until the function is actually called?
  • If so, is there a way to make sure it has access to the local scope of the plugin, even though I assume that, as an object, this function is being passed by reference? I know that I can alter the value of this inside a function by using someFunction.call(valueToUseForThis), but I don’t know whether I can alter its scope after the fact.

Some Stuff that Doesn’t Work

  • Initially, it seemed that if I declared settings in the outer, self-executing function, that would make it available to the defaults. But declaring it there causes problems: if I use my plugin multiple times on a page, the instances get their settings mixed up.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T04:42:52+00:00Added an answer on May 20, 2026 at 4:42 am

    Any attempts to change scope after the fact are doomed. There’s no dynamic scoping in JavaScript.

    Workaround 1.
    Pass settings to the alerter explicitly:

    $.fn.somePlugin = function(userOptions) {
      var settings = $.extend(...);
      settings.alerter(settings)
    }
    
    $.fn.somePlugin.defaults = {
      favoriteColor: 'red',
      alerter: function(settings) { alert("Paint me " + settings.favoriteColor) }
    }
    

    Workaround 2. Pass settings to the alerter implicitly:

    $.fn.somePlugin = function(userOptions) {
      var settings = $.extend(...);
      settings.alerter()
    }
    
    $.fn.somePlugin.defaults = {
      favoriteColor: 'red',
      alerter: function() { alert("Paint me " + this.favoriteColor) }
    }
    

    Both solutions tested and work well with multiple uses of the plugin. If you have no plans for using this keyword in your alerter, you can use it for referencing settings.

    The third valid solution, which you and fudgey mentioned, is to attach settings object to the DOM element itself:

    $.fn.somePlugin = function(userOptions) {
      var settings = $.extend(...);
      $(this).data('somePlugin', {settings: settings})
      settings.alerter.call(this)
    }
    
    $.fn.somePlugin.defaults = {
      favoriteColor: 'red',
      alerter: function() {
          alert("Paint me " + $(this).data('somePlugin').settings.favoriteColor)
      }
    } 
    

    Extra fun. Emulating dynamic scope, a.k.a don’t do this

    function dynamic(fn) {
      var parts = fn.toString().split('{')
      fn = parts.shift() + '{ with (args) { ' + parts.join('{') + '}'
      return {bind: function(args) { return eval('(' + fn + ')') }}
    }
    
    a = b = c = 0
    
    function adder(c) {
      alert(a + b + c)
    }
    
    dynamic(adder).bind({a: 1, b: 2})(3) // alert(6)
    

    Usage in a plugin:

    $.fn.somePlugin = function(userOptions) {
      var settings = $.extend(...);
      dynamic(settings.alerter).bind({settings: settings})()
    }
    
    $.fn.somePlugin.defaults = {
      favoriteColor: 'red',
      alerter: function() { alert("Paint me " + settings.favoriteColor) }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.