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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T13:00:36+00:00 2026-05-22T13:00:36+00:00

I am working on maintaining a ASP.NET MVC application that has the following coding

  • 0

I am working on maintaining a ASP.NET MVC application that has the following coding style. The view has:

<script type="text/javascript">

    $(document).ready(function() {

        SAVECUSTOMERS.init();
    });
</script>

There is a js file included that goes along these lines:

var SAVECUSTOMERS = (function() {

    init = function () {

        $("#saveCust").bind("click", OnSave);
        $("#cancel").bind("click", OnCancel);

    },

    OnSave= function() {

        //Save Logic;

    },

    OnCancel = function() {
                //Cancel logic;

    }

    return { init: init };

})();
  1. Is this a best practices JS coding style? Is the intent to have non obtrusive JS?
  2. What is the SAVECUSTOMERS? I understand that there are different ways of creating classes in javascript (per this link), but this style does not fall into any of those categories listed
  3. Where can I find more information on this style of JS coding?
  • 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-22T13:00:37+00:00Added an answer on May 22, 2026 at 1:00 pm

    1) Using a $(document).ready (or similar function from another library) function is considered standard practice in JavaScript. First of all, it ensures your JavaScript executes on page that has finished evaluating/building it’s DOM. And it also abstracts away some of the browser-implementation inconsistencies when identifying when the DOM is in fact ready. But I assume you are mainly referring to the 2nd code block.

    What you see there is that SAVECUSTOMERS is assigned the result of a self-executing an anonymous function. This is done for a few reasons, the most common being the ability to control the scope and ‘namespace’ of the functions and data inside the anonymous function. This is because JavaScript has lexical scope, and not block level scope.

    The practice of using these self-invoking functions in JavaScript is very common

    However the code itself has several problems. The variables init, OnSave and OnCancel are declared as global variables (because the var keyword was omitted). This largely defeats the purpose of wrapping them in an self-invoking function. Furthermore, the contents of that function are using a mix of object assignment syntax and standard expression syntax, which will result in syntax errors.

    Also, by returning only the init function, the onSave and onCancel functions have been effectively ‘hidden’ or made ‘private’ through the use of closures. This helps keep namespaces clean and encapsulated.

    If I were writing this code (some personal perferences here, there are a few ways to accomplish something simliar), then it would look like this:

    var SaveCustomers = (function($) {
        var init = function () {
            $("#saveCust").bind("click", onSave);
            $("#cancel").bind("click", onCancel);
        };
    
        var onSave = function() {
            //Save Logic;
        };
    
        var onCancel = function() {
            //Cancel logic;
        }
    
        return { init: init };
    
    })(jQuery);
    

    Some notes on the above:

    • I declare variables using the var keyword. This keeps their scope local to this function (you could also technically use named functions declarations as well)

    • I pass jQuery as the parameter in the self-invoking function, and assign it to $ as the argument in the function call. This protects the $ variable inside the function so that we know it references jQuery, and hasn’t been munged by a secondary library that also uses $.

    2) SAVECUSTOMERS is a basic JavaScript object, which has a single owned property called ‘init’, whose value is a function, as defined by the init declaration inside the execution.

    3) Not sure about how to answer this question – your best bet for understanding JavaScript best practices is to read through other JavaScript code that is known to be of quality, such as the jQuery source, or Prototype, or Underscore, etc.

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

Sidebar

Related Questions

I have an old ASP.NET 1.1 site that I am maintaining. We are working
Working with an Oracle 9i database from an ASP.NET 2.0 (VB) application using OLEDB.
We are working on an ASP.NET MVC 3 project and taking advantage of Razor
The context I'm working with a Winforms application (.NET 3.5 SP1) which has the
I am working on site something like job portal in ASP.NET C#. Here I
Working with a SqlCommand in C# I've created a query that contains a IN
Working on a project that parses a log of events, and then updates a
I'm working on a problem similar to that of garbage collection, where I need
My script is working perfect as it stands, but I have been trying to
A friend and I are working on a new project that is using SQL

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.