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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:02:03+00:00 2026-05-23T11:02:03+00:00

I have worked with oop style scripting before and trying to get some kind

  • 0

I have worked with oop style scripting before and trying to get some kind of system with javascript. I wanted to try the most basic pattern, Constructor Pattern.

So I setup one js file called ImageView with a constructor matching the name of the js file.

    function ImageView(){
    alert( 'this is working');
}

Then I set up another js file called Main.js which will be the main instantiation class.

    $(document).ready(function(){
    var imageViewer = new ImageView();
    //ImageView();
});

Now what I don’t get is I can call this object ImageView without even the new constructor call. For example ImageView(). From what I gather this is just another global function and not a encapsulated class. I’m trying to get away from global crap and separate my methods and properties to their own class. What am I missing her.

  • 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-23T11:02:04+00:00Added an answer on May 23, 2026 at 11:02 am

    Others have already answered what the difference is between using new and not using it, so I’ll answer your entirely separate question: how do I avoid globals in JS?

    The answer is that you can’t entirely. You will always have at least one, in which you can stuff your other stuff. So for example if you wanted a “namespace” of xyz, you would do:

    // global:
    var xyz = {}; // or, window.xyz = {} if you are in a browser and want to be more explicit.
    
    // "encapsulated" within the xyz "namespace":
    xyz.ImageView = function () { alert("This is working"); };
    

    There is a better solution: use the emerging concept of JavaScript modules. These are not language features (at least not in the current version of JavaScript), so they are really just hacks introduced by very clever libraries that overwrite a couple of global variables to let you avoid creating any more than the ones provided by those libraries. A good example is RequireJS, where you could do something like the following:

    // In xyz.js, define the xyz module (name automatically derived from filename).
    // Whatever is returned from the function you pass to define is "the xyz module"
    define(function () {
        return {
            ImageView: function () { alert("This is working"); }
        };
    });
    
    // In other code, in a different file, you can say "I require the xyz module
    // to do my work," and pass require a function saying "once you've got the xyz module
    // for me, here's the work I will do with it".
    require(["xyz"], function (xyz) { // dependency array maps to callback arguments
        // I got the xyz module, including the ImageView function it exported. Use it!
        var imageViewer = new xyz.ImageView();
    });
    

    Here the clever globals RequireJS introduces are the functions define and require, but if you use them right, you can avoid ever introducing any further globals beside those two.

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

Sidebar

Related Questions

I have worked on single threaded business logic/back-end programming for most of my career.
I have worked in a SOAP message to get the LoginToken from a Webservice,
I have worked on UVA 10410 Tree Reconstruction several days. But I can't get
I have worked a bit with ASP.NET before. I was using a MS SQL
I have worked with Django before and have recently seen the Play framework .
I have worked on NodeJs and Redis before. Since NodeJs is a web server
I have worked with Databases before; however never designed a Database. For this project
Hello I have worked with regular tables and javascript to add new rows at
I have worked with JQgrid in asp.net mvc, but now i am trying to
I have worked some with Cocos2d for iPhone and find it delightful. I am

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.