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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:46:54+00:00 2026-05-15T10:46:54+00:00

var Test = (function() { return { useSub: function () { this.Sub.sayHi(); }, init:

  • 0
var Test = (function() {
    return {
        useSub: function () {
            this.Sub.sayHi();
        },

        init: function () {
            $(document).ready(this.useSub);
        }
    };
})();

Test.Sub = (function () {
    return {
        sayHi: function () {
            alert('hi');
        }
    };
})();

Test.useSub(); // works
Test.init(); // explodes

Above I am trying to create a Test namespace and add an object Sub to it. I was doing fine until I tried using the object in jQuery. The error is “Uncaught TypeError: Cannot call method ‘sayHi’ of undefined”. If there is a better way to do this, I am open to it.

Edit:

Obviously this was demo code. In my real application the solution that I went with because I think it is the most clear is this one:

var Namespace (function () {
  return {
    init: function () {
      $(document).ready(function() {
        Namespace.onReady();
      }
    },
    onReady: function() {
      alert('Now I am back in the Namespace scope. Proceed as planned');
    }
  };
})();

Edit2: All jQuery callbacks seem to require they are used in this manner or else the scoping is screwed 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-15T10:46:55+00:00Added an answer on May 15, 2026 at 10:46 am

    I think it is a scope problem. If you do

    $(document).ready(this.useSub);
    

    then this.useSub will be executed in the window scope (so inside the function, this refers to the window object) and there doesn’t exist a Sub attribute.

    Try:

    init: function () {
        var obj = this;
        $(function(){obj.useSub()});
    }
    

    For some reason it does not work using $(document).ready(function(){obj.useSub()}); but it works with the $() shortcut.

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

Sidebar

Related Questions

var x= 1; Number.prototype.test = function () { return this }; x.test() === x.test()
why does this only alert 1 ? function test() { var myobj = {
When I run this code: var Test = function() { return this.stuff; }; Test.stuff
If I have: var test = {toString: function(){alert(evil code); return test;}}; how can I
Given this code: function Test(){ var $D = Date.prototype; $D.addDays=function(value){this.setDate(this.getDate()+value);return this;}; $D.clone = function()
Why is this not working? $(document).ready(function(){ var content = '<a href=http://example.com/index.php><b>Some text!</b></a> - <a
Why doesn't this work right? function test() { var start = new Date(2012, 3,
function test(){ if(true){ var a = 5; } alert(a); } test(); I keep getting
Consider a simple JS function as function test(property, color) { var element = document.getElementById(test);
Consider we have a simple cycling Javascript process as: function test() { el=document.getElementById("test"); var

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.