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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:27:47+00:00 2026-06-14T16:27:47+00:00

(function() { var content = [ { title: ‘My awesome blog post’, thumbnail: ‘images/bane.jpg’,

  • 0
(function() {

    var content = [
        {
            title: 'My awesome blog post',   
            thumbnail: 'images/bane.jpg',
        },
        {
            title: 'My second awesome blog post',
            thumbnail: 'images/joker.jpg',
        },
    ],
        template = $.trim( $('#blogTemplate').html() ),
        frag = '';

    $.each( content, function( index, obj ) {  
        frag +=
            template.replace( /{{title}}/ig, obj.title )
                    .replace( /{{thumbnail}}/ig, obj.thumbnail );       
    });

    $('body').append(frag);

})();

Hello There,

I was just wondering why it is that the code block that is contained within a self invoking anonymous function cannot have the following variable declarations prefixed with the var keyword like so:

    var template = $.trim( $('#blogTemplate').html() ),
    var frag = '';

Adding the var keyword to the local variables in this function – template and frag seems to break my code and I really don’t understand why, however if I declare template and frag on a global scope outside of the function as shown below then my code works.

var template;
var frag;

If this relates to scope and anyone has the time to explain it would be much appreciated. Ideally I would like to prefix template and frag with the var keyword to increase readability.

  • 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-06-14T16:27:49+00:00Added an answer on June 14, 2026 at 4:27 pm

    I suspect it is the comma between the variable declarations that is breaking your code, and not the use of the var keyword per se. Try this:

    var template = $.trim( $('#blogTemplate').html() ),
    frag = '';
    

    The var keyword is already applying to template and frag in the first snippet you have provided, because they are the second and third items in a comma separated list of local variable declarations.

    function(){
        var x = 1, 
            y = 2, 
            z = 3;
        window.x; //undefined
        x; //1
        window.y;//undefined
        y; //2
    }
    

    The cleanest way to do this is to eschew the syntactic sugar altogether, and separate each variable declaration into a separate statement:

    var content = [
        {
            title: 'My awesome blog post',   
            thumbnail: 'images/bane.jpg',
        },
        {
            title: 'My second awesome blog post',
            thumbnail: 'images/joker.jpg',
        }
    ];
    var template = $.trim($('#blogTemplate').html());
    var frag = '';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

$.get($(this).attr('id'), function(data){ var qp_post = $(data).filter('title'); alert(qp_post); }); This fetches the content fine however
I'm creating a query for mongodb: app.get('content/:title', function(req, res) { var regexp = new
EDIT : Per Slaks $j('#banner-content img').each(function() { var link = $j('#page-tabs li a.' +
I have some code like this: var content = document.getElementById('myDivId'); function MyFunction() { alert(content.style.height);
function sc_HTMLParser(aHTMLString){ var parseDOM = content.document.createElement('div'); parseDOM.appendChild(Components.classes['@mozilla.org/feed-unescapehtml;1'] .getService(Components.interfaces.nsIScriptableUnescapeHTML) .parseFragment(aHTMLString, false, null, parseDOM)); return parseDOM;
var Modal = function (content, options) { this.options = options this.$element = $(content) .delegate('[data-dismiss=modal]',
$(function(){ $('.page').click(function() { var paging = $(this).attr(name); var dataString = 'page='+paging; $.ajax({ type: POST,
$(#note_content).dialog({ title: Note, modal: true, width:'auto', height:'auto', resizable:false, open: function(){ var note_text = $('#note_content').attr('note_text');
$(#body_content).dialog({ title: AAAa, modal: true, width:'auto', height:'auto', resizable:false, open: function(){ var id = $('#right_button').attr('id');
$(function() { var number = document.getElementById(breuken); var i=0; for (i=1;i<=10;i++){ var fRandom = Math.floor(Math.random()*10);

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.