74166

What is the difference between assigning an IIFE's public members to a variable vs returning an

Question:

I've been looking at a lot of JavaScript code lately and I've seen two different ways of using assigning "public" properties of IIFE's.

The first is to create a variable and assign that variable to a property inside of the IIFE like so:

var public1; (function(){ var foo= "Foo", bar= "Bar"; public1= { getFoo: function(){ return foo; } }; }());

The second way I see is returning an object from the IIFE like so:

var public2 = (function(){ var foo2= "Foo2", bar2= "Bar2"; return { getBar: function(){ return bar2; } }; }());

Is there a fundamental difference between these two ways or is it just a matter of preference? I've also created a fiddle so you can run or update the code if you'd like: <a href="http://jsfiddle.net/bittersweetryan/gnh79/4/" rel="nofollow">http://jsfiddle.net/bittersweetryan/gnh79/3/</a>

Answer1:

There is no difference.

But I'd argue that the second one is a bit easier to maintain. When you change the variable name in the first example, you have to change it in the function as well.

Recommend

  • Simple Distributed Erlang
  • calling a fragment from fragment
  • HighCharts - Show tooltip on column where value is 0 or null
  • Specifying virtual keyboard type for EditText in XML
  • Why does the following throw an “Object doesn't support property or method 'importNode
  • AWS-SES: Handling Bounces for Invalid ISPs
  • Why doesn't a local variable live long enough for thread::scoped?
  • Dynamically add UI elements to StackPanel after button pressed
  • C# List of Panels
  • Django model for a Postgres view
  • How to retrieve information from antrun back to maven?
  • How to use jQuery's $.post() method with async/await and typescript
  • Querying row counts segregated by date ranges
  • Firefox Extension - Monitor refresh and change of tab
  • How can the INSERT … ON CONFLICT (id) DO UPDATE… syntax be used with a sequence ID?
  • WPF - CanExecute dosn't fire when raising Commands from a UserControl
  • chrome.tabs.executeScript only fires when the Developer Console is open
  • Meteor helpers not available in Angular template
  • Change multiple background-images with jQuery
  • Align navbar back button on right side
  • Window Size for Mac application
  • Build own AppleScript numerical error handling
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • How to delete a row from a dynamic generate table using jquery?
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • C# - Getting references of reference
  • using HTMLImports.whenReady not working in chrome
  • How do I configure my settings file to work with unit tests?
  • Authorize attributes not working in MVC 4
  • Is it possible to post an object from jquery to bottle.py?
  • EntityFramework adding new object to nested object collection
  • Django query for large number of relationships
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • How can I use `wmic` in a Windows PE script?
  • java string with new operator and a literal
  • Net Present Value in Excel for Grouped Recurring CF
  • How to push additional view controllers onto NavigationController but keep the TabBar?
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?