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

  • SEARCH
  • Home
  • 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 8701491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:24:39+00:00 2026-06-13T02:24:39+00:00

Ok I am definitely learning a lot when it comes to JavaScript scope functions

  • 0

Ok I am definitely learning a lot when it comes to JavaScript scope functions and module patterns, awesome stuff! Right now I’m teaching myself to pass jQuery into a scope function this way it loads sooner, and if for some reason I had another framework that used $, there will be no confusion.

But what I don’t fully understand is when to create a “new” instance “in context to scope functions” when I want to pass in jQuery. Here is what I mean…If I was going to use the following as a base, it will return pubs, which can be associated to a function or properties, etc, I get it.

var DemoA = (function($) {
var pubs = {};
pubs.dosomething = //some function that calculates cool stuff with help of jquery
return pubs;
})(jQuery);

Now when I try to create a new instance….

var stuff = new DemoA();

…I get an error through Google Chrome Developer Tools. It says “object is not a function” or something to that effect. But if I call DemoA directly like this…

DemoA.dosomething();

…then everything works fine. What is going on here? and why can’t I create a new instance variable?

Thanks in advance for helping me get smarter!

  • 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-06-13T02:24:40+00:00Added an answer on June 13, 2026 at 2:24 am

    Look at your return statement. You’re returning an object that looks like this:

    {"dosomething": function () { }}
    

    You can’t create a new instance of an object. You can call dosomething directly because it’s an immediate property (method) of the returned object.

    I think you want something like this:

    http://jsfiddle.net/veJqg/

    var DemoA = (function($) {
        var pubs = function () {
            this.dosomething = function () {
                console.log("just executed `dosomething`");
            };
        };
        return pubs;
    })(jQuery);
    
    var a = new DemoA();
    a.dosomething();
    

    This way, you are still aliasing the jQuery object as $, and you are returning a function that can be used in the way you want.

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

Sidebar

Related Questions

I'm definitely still learning Javascript so please forgive me if this is a very
Looking at Stani's Python IDE , it definitely comes bundled with tons of useful
Something I don't enjoy about programming is learning a new API. For example, right
I’ve started learning Erlang a few days ago and it’s definitely a very interesting
Very confused over this. I've just started learning about pointers and have now decided
Okay, so I'm just learning about templates. Anyways, I'm probably (most definitely) doing something
Javascript definitely isn't my strongest point. I've been attempting this for a couple of
Okay this is definitely a n00b question but here goes. The way I understand
I'm definitely missing something. I have an XML file that stores HTML snippets, as
I'm definitely a newbie to ruby (and using 1.9.1), so any help is appreciated.

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.