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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:23:00+00:00 2026-05-23T12:23:00+00:00

I’ve been playing with the code associated with this really cool article. In the

  • 0

I’ve been playing with the code associated with this really cool article.

In the articles code a variable is assigned a function as follows:

var messageFactory = (function() {
    var that = {},
        $chatMessage = $('<p></p>').
          addClass('chat message'),
        $nick = $('<span></span>').
          addClass('nick'),
        $systemMessage = $('<p></p>').
          addClass('system message');

    var chat = function(message) {
      var $filledNick = $nick.clone().
            text(message.nick + ':');
      return $chatMessage.clone().
        append($filledNick).
        append(message.text);
    };

    var system = function(message) {
      return $systemMessage.clone().text(message.text);
    };

    that.chat = chat;
    that.system = system;

    return that;
  })();

Later sub functions are called like the following,

messageFactory.system({ text: 'You changed your nick to ' + nick + '.'})

and

messageFactory.chat({ nick: 'me', text: message })

What’s going on in those calls? Specifically it appears that the var messageFactory is working similar to a class definition in language like C#, and I’m missing the scope related mechanisms to how the values are being passed via the object { text: ‘…’ …}.

Thanks much!

  • 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-23T12:23:01+00:00Added an answer on May 23, 2026 at 12:23 pm

    The first and arguably most important thing to note is the last line. Specifically, the () before the ;. What this is doing is immediately executing the anonymous function on line 1. This is important to note because messageFactory will not contain the anonymous function, but rather whatever is returned from it. To better understand that, I’ll give an example…

    var x = (function(){ return "Hello!"; })();
    // x will contain "Hello!", not the function.
    

    The second thing to keep in mind is that objects in Javascript will maintain a reference to the closure that they were created in. So if you immediately execute a function like we’re doing above, that will a form a closure and objects created in that closure will maintain a reference to it even after the function is done executing. Another example….

    var sayHi = (function(){ 
        var salutation = "Hello";
        return function(name) {
           return salutation + ", " + name + ".";
        }
    })();
    

    Note again that we have an anonymous function being immediately executed. So the variable sayHi will not contain the outside anonymous function, but rather it’s return value. So sayHi will actually contain function(name){ return salutation + ", " + name + ".";}. You’ll note that we’re not passing in salutation, but we can still access it as it’s part of the closure that this function was created in.

    The final thing to understand the provided code is that in Javascript, {} is an Object literal. It’s essentially equivalent to saying new Object(). These objects can have properties and methods the same as a C# object, which is where the .text is coming from that you referred to.

    On line 2, the code is creating an Object literal: var that = {}. The next var creation is var chat = function(message){.... where a function is created which takes a message parameter and does some stuff with it. Towards the end of the code, that chat function is then assigned to the chat property of that and then that is returned: that.chat = chat and return that.

    The point of all this is that messageFactory does not contain the function it appears to be assigned to, but rather the return of that function. In this case, that return is actually the that Object, which has two properties of chat and system. Those properties actually point back to the chat and system variables inside the same closure that that was created in, which makes this all work.

    The final piece is simple enough…. when you call messageFactory.chat({ text: 'something', nick:Joe}) you’re actually passing an Object as a parameter to the chat function inside the closure. It then references the object’s nick and text properties to return its result.

    Hope that helps. I’m not sure I explained it very well, but that’s kind of how it works in my head.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
Does anyone know how can I replace this 2 symbol below from the string
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I

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.