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 5842261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:59:41+00:00 2026-05-22T11:59:41+00:00

I am having the following plugin: (function($) { $.fn.myPlugin = function(options) { var opt

  • 0

I am having the following plugin:

(function($) {
    $.fn.myPlugin = function(options) {
    var opt = $.extend({}, $.fn.myPlugin.defaults, options);

        if (!opt.a) {
            console.log('a is required!');
            return false;
        }
        if (!opt.b) {
            console.log('b is required!');
            return false;
        }
        if (!opt.c) {
            console.log('c is required!');
            return false;
        }
        //Rest of the logic
    }
    $.fn.myPlugin.defaults = {
    };
});

Now this plugin will be called from outside like this:

$('div.x').myPlugin({
    a:'aa',
    b:'bb',
    c:'cc'
});

As you can see from the plugin I need a, b and c options from outside i.e they are compulsory. But there can 10-15 compulsory options and this code

        if (!opt.a) {
            console.log('a is required!');
            return false;
        }
        if (!opt.b) {
            console.log('b is required!');
            return false;
        }
        if (!opt.c) {
            console.log('c is required!');
            return false;
        }

can become lengthy and cumbersome. Is there any shorter or smarter way to write this? I was thinking of some common code.

  • 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-22T11:59:42+00:00Added an answer on May 22, 2026 at 11:59 am

    If there are that many, you could put them in an array and check that way:

    var required = ['a', 'b', 'c'];
    var index, optname;
    for (index = 0; index < required.length; ++index) {
        optname = required[index];
        if (!(optname in opt)) {
            console.log(optname + " is required");
            return false;
        }
    }
    

    Note that I’ve gone for an if (!(optname in opt)) check there (rather than if (!opt[optname]) as you originally had), to allow for options that must be specified but for which 0, false, undefined, or other falsey values are valid. The in check sees whether the option is there without worrying about its value being truthy.


    Slightly off-topic: You might choose to wait to fail until you’ve checked for all of the properties, as @Marcus points out in the comments. Also, you might consider throwing an exception rather than returning false, as someone failing to specify the options correctly should be an exceptional condition… But those are minor points.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble implementing this jQuery plugin for dynamic forms while following this tutorial
I have the following code (function($) { // carousel var Carousel = { settings:
I'm following the guidelines for plugin authoring on jquery's website, but I'm having trouble
Using this hotkeys plugin, https://github.com/jeresig/jquery.hotkeys , I'm having some problems with the following code:
Imagine having the following scenario: You need to create a system where the Back
I'm having the following problem. I created a application context file for spring.net. The
I'm having trouble getting the following to work in SQL Server 2k, but it
I'm having trouble trying to optimize the following query for sql server 2005. Does
I'm having problem in the following line: rd.PrintOptions.PaperSize = PaperSize.PaperFanfoldStdGerman; it throws an exception
I'm having an issue with the following code: private void DataPortal_Fetch(TaskCriteria criteria) { using

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.