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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:11:39+00:00 2026-05-26T23:11:39+00:00

Let us say I have the following: var foo = (function(){ var bar =

  • 0

Let us say I have the following:

var foo = (function(){
    var bar = 0;
    return {
       getBar: function(){
           return bar;
       },
       addOne: function(){
           bar++;
       },
       addRandom: function(rand){
           bar += rand;
       }
    }
})();

And I have the following:

var foo2 = function(){
    var bar = 0;
    this.getBar = function(){
           return bar;
       };
    this.addOne = function(){
           bar++;
       };
    this.addRandom = function(rand){
           bar += rand;
       }
};

Is the only difference in executing the functions a new?

alert(foo.getBar()); //0
foo.addOne();
foo.addRandom(32);
alert(foo.getBar()); //33

var foo2_obj = new foo2;
alert(foo2_obj.getBar());//0
foo2_obj.addOne();
foo2_obj.addRandom(32);
alert(foo2_obj.getBar());//33

They both out put the exact same thing.

So what is the difference in the long run?

What can one do that the other cannot?

Fiddle Demo of the above: http://jsfiddle.net/maniator/YtBpe/

  • 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-26T23:11:39+00:00Added an answer on May 26, 2026 at 11:11 pm

    In the first one you can only create the object once, while with the second one you can create as many objects as you like. I.E. the first one is effectively a singleton.

    Note that closures are not ok for the second one. Every time you instantiate it you are creating the functions all over again and waste a ton of memory. The prototype object is intended to counter this, where you can create the functions once outside a function scope and no accidental closures are created.

    function foo2(){
        this._bar = 0;
    }
    
    foo2.prototype = {
    
        constructor: foo2,
    
        getBar: function(){
            return this._bar;
        },
    
        addOne: function(){
            this._bar++;
        },
    
        addRandom:function(rand){
            this._bar += rand;
        }
    
    };
    

    Then:

    var a = new foo2, b = new foo2, c = new foo2;
    

    Creates three instances which have their own _bar but share the same functionality.

    jsperf

    You can “compare” all of this to PHP, some of the code won’t even run but it’s “equivalent” in principle:


    var foo = (function(){
        var bar = 0;
        return {
           getBar: function(){
               return bar;
           },
           addOne: function(){
               bar++;
           },
           addRandom: function(rand){
               bar += rand;
           }
        }
    })();
    

    is roughly “equivalent” to this in PHP:

    $foo = new stdClass;
    
    $foo->bar = 0;
    
    $foo->getBar = function(){
        return $this->bar;
    };
    
    $foo->addOne = function(){
        $this->bar++;
    }
    
    $foo->addRandom = function($rand){
        $this->bar += $rand;
    }
    

    var foo2 = function(){
        var bar = 0;
        this.getBar = function(){
            return bar;
        };
        this.addOne = function(){
            bar++;
        };
        this.addRandom = function(rand){
            bar += rand;
        }
    };
    

    Is roughly “equivalent” to this in PHP:

    Class foo2 {
    
    
        public function __construct(){
        $bar = 0;
    
            $this->getBar = function(){
                return $bar;
            };
            $this->addOne = function(){
                $bar++;
            };
            $this->addRandom = function($rand){
                $bar += rand;
            };
    
    
        }
    
    }
    

    function foo2(){
        this._bar = 0;
    }
    
    foo2.prototype = {
    
        constructor: foo2,
    
        getBar: function(){
            return this._bar;
        },
    
        addOne: function(){
            this._bar++;
        },
    
        addRandom:function(rand){
            this._bar += rand;
        }
    
    };
    

    Is roughly “equivalent” to this in PHP:

    Class foo2 {
    
        public $_bar;
    
        public function __construct(){
            $this->_bar = 0;    
        }
    
        public function getBar(){
            return $this->_bar;    
        }
    
        public function addOne(){
            $this->_bar++
        }
    
        public function addRandom($rand){
            $this->_bar += $rand;
        }
    
    }
    

    …and is the only one that is close to OOP in the three above examples


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

Sidebar

Related Questions

Let's say I have the following HTML: <table id=foo> <th class=sortasc>Header</th> </table> <table id=bar>
Let's say I have following typed in my source file. var myFunction = function()
Let's say we have the following JavaScript/jQuery code below function createElement(i, value) { return
So..Let's say I have the following code snippet function Rectangle(w,h){ this.width = w; this.height
Let's say we have following this: <p class=first>This is paragraph 1.</p> <p class=second>This is
Let say that we have the following query: SELECT DISTINCT COUNT(`users_id`) FROM `users_table`; this
Let's say we have the following method declaration: Public Function MyMethod(ByVal param1 As Integer,
Let's say I have the following class X where I want to return access
In JavaScript let's say we have the following code var test = 'd'; if
Let's say I have the following list: var someList = new List<SomeObject>(); Which has

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.