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

The Archive Base Latest Questions

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

For Javascript application I need to be able to let the user save the

  • 0

For Javascript application I need to be able to let the user save the state of an object. This involves saving a set of custom functions that were previously created dynamically or through a GUI, and loading these stored functions later. Essentially I need to serialize and unserialize functions.

Right now I achieve the serialize part by using the Function object’s .toString() method:

func.toString().replace('"', '\"').replace(/(\r)/g, ' ').replace(/(\n)/g, ' ').replace(/(\t)/g, ' ')

This gives me “serialized” functions like this beauty (note that the function is unnamed):

"function (someParameter) {this.someFunctionName(return 'something';}"

Unserializing this is where I struggle. My current best solution for unserializing the functions is this:

var func = eval( 't =' +  so.actions[i].func);

Note how I prepend the serialized function with t = before calling eval() on it. I don’t like doing this because it creates a global variable but I cannot find a way around it. When not prepending this, I receive a “SyntaxError: Unexpected token (“. When prepending var t =, eval() does not return the function but undefined.

Are there alternative ways to “unserialize” an unnamed function?

PS: I am aware of the security implications of using eval() on user input. For the foreseeable future I am the only user of this software, so this is currently a non-issue.

  • 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-26T06:37:13+00:00Added an answer on May 26, 2026 at 6:37 am

    You could use the Function constructor to avoid creating a global variable:

    var func = Function("return " + so.actions[i].func)();
    

    This is equivalent to:

    var func = eval("(function () { return " + so.actions[i].func + " })()");
    

    One thing to be wary of is the surrounding closure which you won’t be able to capture, so variables inside the function will “change” when you serialize and de-serialize.

    edit

    I’m not thinking, you can of course just do:

    var func = eval("(" + so.actions[i].func + ")");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need JavaScript working almost perfectly in my application that should be able to
I need a Javascript application that, when run, prompts a password to be entered,
I need to build my application GUI using HTML/CSS/JavaScript with a C++ backend all
For a project that will let developers add their own Javascript applications, I need
I'm developing an application with javascript. What I need is to have divs with
I'm creating a web application in which teachers need to be able to easily
I'm making a large and complex application and I need to set tabindexes to
I'm currently developing a silverlight application with prism. From this application I need to
Possible Duplicate: How can I pre-set arguments in JavaScript function call? (Partial Function Application)
Background I want to be able to parse Javascript source in a Delphi Application.

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.