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

The Archive Base Latest Questions

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

I have this code: var obj = function (i) { this.a = i; this.init

  • 0

I have this code:

var obj = function (i) {
   this.a = i;
   this.init = function () {
       var _this = this;
       setTimeout(function () {
           alert(_this.a + ' :: ' + typeof _this);
       }, 0);
   };
   this.init();
};

obj('1');
obj('2');
obj('3');
new obj('4');​​​

http://jsfiddle.net/kbWJd/

The script alerts ‘3 :: object’ three times and ‘4 :: object’ once.

I know why this is. It because new obj('4') creates a new instance with it’s own memory space and the calls prior share their memory space. When in the code of obj how can I determine if I am a new object or a function, since typeof _this just says ‘object’?

Thanks.

  • 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:03:49+00:00Added an answer on June 6, 2026 at 5:03 pm

    Is this what you’re looking for? If you execute a function without the new keyword this inside the function equals the containing object (window in this case).

    if( this === window ){
        console.log('not an object instance');
    } else {
        console.log('object instance');
    }
    

    Example with different containing object:

    var obj = {
    
        method: function(){
    
            if( this === obj ){
                alert('function was not used to create an object instance');
            } else {
                alert('function was used to create an object instance');
            }
    
        }
    
    };
    
    
    obj.method(); // this === obj
    
    new obj.method(); // this === newly created object instance
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this piece of code: this.json.each(function(obj, index) { var li = new Element('li');
I have this code (JSFiddle) var OBJ = function(){ var privateVar = 23; var
Imagine I have this code: var myFunc1 = function(event) { alert(1); } var myFunc2
For example I have this plugin code: jQuery.fn.keys = function(obj) { var keys =
I have figure out this code function getAllProperties(obj){ var result={ properties:[], methods:[] }; var
I have some code that looks like this: foreach(var obj in collection) { try
i have this code: var list = new List<int>(); for(int i=0;i<10;i++) list.Add(i); for(int i=0;i<10;i++)
I have this code: <script type=text/javascript> $(document).ready(function() { var url = https://graph.facebook.com/search?q=cinema&type=post; $.ajax({ type:
I have this code: $(document).ready(function() { var url = https://graph.facebook.com/search?q=cinema&type=post; $.ajax({ type: POST, url:
I have this code to fetch profile pictures and messages: $(document).ready(function() { var url

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.