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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:27:13+00:00 2026-05-26T11:27:13+00:00

I am having troubles with passing an object to a function using setTimeout; function

  • 0

I am having troubles with passing an object to a function using setTimeout;

function alertObject(obj){
   alert(obj); //This is supposed to display "[object Object]"
}

function startCountdown(){
   var myObj = new myClass();
   setTimeout("alertObject("+ myObj +")",1000);
}

startCountdown();

When I run this, I get an error saying “unexpected identifier”. I know I can use an anonymous function like this;

setTimeout(function(){alertObject(myObj)},1000);

Instead of

setTimeout("alertObject("+ myObj +")",1000);

But the thing is I want to know why you can not pass an object using the eval() function. It works with strings…

  • 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-26T11:27:14+00:00Added an answer on May 26, 2026 at 11:27 am

    You can’t serialise an object whilst maintaining the identity of that object. (In any language, not just JavaScript.)

    'alertObject('+myObj+')' involves turning the object into a string with toString(), resulting in alertObject([object Object]) which is clearly not valid JavaScript.

    You can provide a toString() implementation that returns something that is valid JavaScript, and use that to create a new object that is like the original object:

    function myClass(num) {
        this.num= num;
    }
    myClass.prototype.toString= function() {
        return 'new myClass('+this.num+')';
    };
    
    var a= new myClass(3);
    var b= eval(''+a);     // 'new myClass(3)'
    alert(a.num===b.num);  // true
    

    but it isn’t the same object instance:

    alert(a===b); // false
    

    and there’s no way to get the actual original object, short of, for example, keeping a lookup of every instance of the object, and passing a key to that lookup.

    Hiding code in strings sucks. This is one of the reasons you should never use setTimeout with a string argument. Go with passing the the function object in.

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

Sidebar

Related Questions

I am having some troubles passing a reference to an object which is of
I'm having troubles using Blend with my visual studio solution. In Runtime, and compile
I am using the following code to build object for posting via ajax: eval('var
I'm not sure why, but I'm having trouble passing a data object (NSManagedObject) to
I'm using BlazeDS to connect Flex with Java. I'm having trouble passing ArrayLists of
I'm having some trouble with JavaScript and the passing of a function as parameter
I'm having trouble passing a float value from one object to another. It appears
I'm having some trouble passing a number as an argument for a method: -
CakePHP Newbie :) I am having trouble accessing another controller and passing that data
As title says, im having trouble with my junit tests passing for checking if

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.