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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:23:15+00:00 2026-06-06T17:23:15+00:00

For some time now, I’ve been using the design pattern of objectOne, shown below.

  • 0

For some time now, I’ve been using the design pattern of objectOne, shown below. I cannot remember where I picked it up. I tried to find it but couldn’t. Perhaps its some hybrid of things I read about. Today I discovered that it is very flawed, since this is resolving to the window object, making all public methods global. I was under the impression that when this is used within a function, it would refer to the function itself, as opposed to the global window object. I guess this is not the case? Could someone explain something that I’m missing or point me to a resource that explains it? I’m also interested in either fixing this pattern or finding a similar one that doesn’t have this problem with global method names. I suppose if I would use a variable other than this, perhaps fn, and i return that, then it would fix things. Thanks in advance for any help with this question, sorry its sort of vague.

JS Fiddle:
http://jsfiddle.net/nLL8y/3/

myapp = {};

myapp.objectOne = function() {
    var that = this,
        p = {};

    this.public = function() {
        console.log(this);
    };

    p.private = function() {};

    return this;
}();

myapp.objectTwo = {
    public: function() {
        console.log(this);
    },

    notPrivate: function() {}
};

myapp.objectThree = function() {
    var fn = {},
        p = {};

    fn.public = function() {
        console.log(this);
    };

    p.private = function() {};

    return fn;
}();

//creates global functions
myapp.objectOne.public();
//doesn't allow private
myapp.objectTwo.public();​
//seems to work
myapp.objectThree.public();​
  • 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-06T17:23:16+00:00Added an answer on June 6, 2026 at 5:23 pm

    myapp is used as a namespace in your example. objectOne and objectTwo are constructors so they should start with capital letter. But your biggest problem is using methods directly as opposed to creating objects:

    var myapp = {};
    
    myapp.ObjectOne = function() {
        this.public = function() {
            console.log(this);
        };
        var private = function() {};
    };
    
    myapp.ObjectTwo = function() {
        this.public = function() {
            console.log(this);
        },
    
        this.notPrivate = function() {}
    };
    
    
    var o1 = new myapp.ObjectOne();
    o1.public();
    
    var o2 = new myapp.ObjectTwo();
    o2.public();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been using FB Connect for some time now. The code below has worked
I've been using NSViewAnimation for some time now and I've read somewhere that Core
I've been using JPA for some time now and been in projects where we've
For some time now I've been using the following code to dynamically write in
Ive been using MSVC2010 for some time now combined with Python Tools 1.0 I'm
I have been reading SO for some time now, but I truly cannot find
I have been using Grails for some time now, but in school they are
I've been using git for some time now and understand how commits work, tags,
I've been using MySQL for quite some time now. Most of that time I
Hi I have been using Emacs23 for some time now and find it a

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.