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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:23:16+00:00 2026-05-27T01:23:16+00:00

How can I tell closure compiler that an anonymous function should not be removed

  • 0

How can I tell closure compiler that an anonymous function should not be removed as dead code? I do not want to store the function to the window object. I am looking for an alternative technique.

Say I have a javascript file that contains one giant anonymous function.

(function(){return "I am here!"})

For usage, I download this function via ajax and then capture the output of eval using something like

var f = eval('(function(){return "I am here!"})');

This assigns the anonymous function in the variable f.

d8> var f = eval('(function(){return "I am here!"})');
d8> f
function (){return "I am here!"}
d8> f()
I am here!

When I run the compiler over my js file that contains a single large anonymous function, the compiler “optimizes” my code by outputting nothing. This is obviously not what I want. Is there some sort of JSDoc I can put above my function to mark is as not being dead code?

~~~~~~~~~~~~~~~~~~~~~~~~

Work around: (This isn’t really what I wanted, but it seems to be the best result with the tools available)

~~~~~~~~~~~~~~~~~~~~~~~~

I ended up using a combination of John and David’s answers:

// code would be provided from an ajax request
var code = 'function F(){return "I am here!"};'

code = "("+code.replace(/\s*\;\s*$/, "")+");"

f = eval(code);

Giving the function a name makes the Closure Compiler happy.

Depending on the function, closure compiler will define variables outside of the function so I wrap the code in parenthesis to avoid defining global variables.

The replace method just moves the last semicolon used to separate the function definition so that it doesn’t cause a syntax error.

  • 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-27T01:23:16+00:00Added an answer on May 27, 2026 at 1:23 am

    You could store the function in the file without the wrapper, which would prevent the compiler from ‘optimising’ it, and then add the function wrapper when you eval it.

    var code = 'return "I am here!";';
    var f = eval('(function(){' + code + '})');
    

    If your code really is ‘invalid’ without the function wrapper, you could leave it wrapped, but execute the wrapper, then continue as above. For example:

    var code = '(function(){return "I am here!";}())';
    var f = eval('(function(){return ' + code + '})');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When use the Google Closure Compiler advanced optimizations on the following code: function add(v1,
I want to convert function object to function. I wrote this code, but it
If I run this piece of code through closure compiler or uglifyjs, this.init is
In JS, we can write closure like: function f(){ var a=0; function g(){ alert(a++);
The Closure Compiler can use data type information about JavaScript variables to provide enhanced
Can someone tell me why this Grails domain class will not compile (at runtime)?
Can anyone tell me, or send me to a page, that can tell me
We can tell bots to crawl or not to crawl our website in robot.txt.
Near as I can tell, CouchDB revisions are not to be treated like revisions
I can tell my page to use a certain CultureInfo like System.Globalization.CultureInfo.CreateSpecificCulture(en-US); The code

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.