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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:05:24+00:00 2026-05-24T08:05:24+00:00

If I run this piece of code through closure compiler or uglifyjs, this.init is

  • 0

If I run this piece of code through closure compiler or uglifyjs, this.init is not shortened.. Can anyone tell me why that is?

function test() {
    var v = "abc";
    this.init = function() {
         alert('var = ' + v + ' and func = ' + f());
         f2();
    }
    function f() {
        return 'def';
    }
    function f2() {
        v = "ghi";
        alert('blabla');
        alert('filler');
    }
}

test();

uglifyjs turns this into :

function test(){function c(){a="ghi",alert("blabla"),alert("filler")}function b(){return"def"}var a="abc";this.init=function(){alert("var = "+a+" and func = "+b()),c()}}test()

Beautified that is:

function test() {
    function c() {
        a = "ghi", alert("blabla"), alert("filler")
    }
    function b() {
        return "def"
    }
    var a = "abc";
    this.init = function () {
        alert("var = " + a + " and func = " + b()), c()
    }
}

test()

So why is this.init() not changed as well to a shorter name?

Additionally, what exactly is the difference between doing:

 function init() {..}

and

 this.init = function() { .. }

Thanks,
Wesley

  • 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-24T08:05:26+00:00Added an answer on May 24, 2026 at 8:05 am

    init is not shortened for the same reason test is not shortened … because it is part of the public API of your code.

    When you call var t = new test() you would create an object that looks like this:

    {
        init: function() { ... },
        prototype: test
    }
    

    and you can call t.init(). If the compiler did not respect those variables which are available from the global scope, you would have to inline all of your JavaScript code into one file before you minified it. Otherwise, every time you minified test.js the public function test‘s name would change. So this code:

    <script type="text/javascript" src="js/test.min.js"></script>
    <script type="text/javascript">
    var t = new test();
    t.init();
    </script>
    

    would break (because test would probably be changed to a by the minifier and init to some other letter.)

    As to the second part of your question, when you do this.init = function you are setting an attribute on an indeterminate object (it could be anything, since this is set at call time in JavaScript and you are declaring it inside of the function test). When you write function init(){} you are writing a function declaration, which will be hoisted to the top of the enclosing scope (meaning you can call it, inside of the scope in which it is defined, before you have defined it.) However, it will not be available outside of test.

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

Sidebar

Related Questions

When I run this code the selected item is not visible. I've already tried
I was reading through some threading related code and found this piece of code:
I have a piece of code that I've haphazardly written in order to run
when i run this code : CONADefinitions.CONAPI_FOLDER_INFO2 FolderInfo; int iResult = 0; IntPtr Buffer
I'm working with a MySQL query that writes into an outfile. I run this
I receive this Run-Time Check Failure upon the return in the following code. I
Has anyone run into this error message before when using a timer on an
Recently I found a piece of code that creates an instance of TButton from
I have this piece of code in my Silverlight project: private void button1_Click(object sender,
I have a piece of code like this (Perl file): print 9 + 16

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.