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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:26:53+00:00 2026-06-03T07:26:53+00:00

I have got a class (function) in js named Foo that create objects. As

  • 0

I have got a “class” (function) in js named Foo that create objects. As it is used very frequently, I want it to avoid requiring the new keywoard being used when new instances of it are to be created: Foo(something); instead of new Foo(something);.

I got this to work in Firefox with:

function Foo(arg) {
    if (this instanceof Window)
        return new Foo(arg);

    //Object construction here.
    this.bar = "Hello " + arg;
}

Now I can create instances of Foo just by calling it as a function.

console.log(Foo("World").bar); //Output "Hello World" in console.

While this is working in FF, it does not in Chrome and I did not dare testing IE yet.

The problem in chrome is that window is really of type DOMWindow in chrome

Uncaught ReferenceError: Window is not defined

and this instanceof DOMWindow does not work in chrome because for some reason it gives:

ReferenceError: DOMWindow is not defined

I have also tried using !(this instanceof Foo) and typeof this always seems to give "object".

How can I reliably detect if the new keyword was omitted when calling Foo on all browsers?

Update: !(this instanceof Foo) does work, I just had a stray return this in my real Foo function.

  • 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-03T07:26:55+00:00Added an answer on June 3, 2026 at 7:26 am

    Testing this being an instanceof Foo works in Chrome 20, FF 12, and IE 8, and should work fine:

    function Foo(arg) {
        if (!(this instanceof Foo)) return new Foo(arg);
    
        //Object construction here.
        this.bar = "Hello " + arg;
    }
    
    var foo = Foo('World');
    var baz = new Foo('Baz');
    
    console.log(foo.bar);
    console.log(baz.bar);
    

    As a fiddle : http://jsfiddle.net/YSEFK/

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

Sidebar

Related Questions

I have got a designing dilema. I have a class named UserAccount that represent
In my project I have a function named GetObject that is wrapped in one
So I've got a class that I'd like to have it just set defaults
I've got a class function that needs to pass through a particular keyword argument:
I've got a philosophical programming problem. Let's say I have a class named Employees.
I have got a template class as follows: class MyClass<T> { T field; public
I have got the following class structure, and have plenty of classes like C
I've got the class object for an enum (I have a Class<? extends Enum>
I've got a class with many string arrays. I'd like to have one generic
i've got 3 classes (all deriving from the same base class) and i have

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.