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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:13:35+00:00 2026-05-28T00:13:35+00:00

I have the following code 1) var x = {}; x.data = {name: ‘name’,

  • 0

I have the following code

1)  var x = {};
    x.data = {name: 'name', lname: 'lname'};
    x.someOtherData = ['blah','blah','irrelevant'];
    aFunction(x);

So I call the above group of statements several times, order of the statements being important ofcourse. I sometimes forget to do the last statement aFunction(x) which has caused me a lot of headaches, so I’ve done the following:

2) aFunction(function(){
    var x = {};
    x.data = {name: 'name', lname: 'lname'};
    x.someOtherData = ['blah','blah','irrelevant'];
    return x; 
   }());                     

Which works and I was curious if there was a way of enforcing in my function method, aFunction, that the parameter being passed must be a an anonymous function. I am returning an object from that anonymous function so the following will obviously not work

3) function aFunction(x) {
    if(x.constructor == Function) {
      alert('yay you're doing it right'); 
    } else { 
     alert('nay, you're doing it wrong'); //this is what happens given that x.constructor == Object
   }
}

I know I can do the following and the above check will work, but I would like to enclose all my logic inside aFunction argument, in the parenthesis, as in code snippet 2:

4) var z =  function(){
   var x = {};
   x.data = {name: 'name', lname: 'lname'};
   x.someOtherData = ['blah','blah','irrelevant'];
   return x; 
  };
  aFunction(z);  

Any ideas?

Update:

Nevermind I figured it out. I can just not call the anonymous function immediately as I did and call it in aFunction method. Sure helps to write out your problems in stack overflow so that the problem and solution is clearer. Still open to how others solve similar problems. Design patterns etc..

Solution:

aFunction(function(){
var x = {};
x.data = {name: 'name', lname: 'lname'};
x.someOtherData = ['blah','blah','irrelevant'];
return x; 

});

function aFunction(x) {
if(x.constructor == Function) {
      alert('yay you're doing it right'); 
      x(); //call it here
    } else { 
     alert('nay, you're doing it wrong'); 
   }

}

  • 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-28T00:13:36+00:00Added an answer on May 28, 2026 at 12:13 am

    I was curious if there was a way of enforcing in my function method, aFunction, that the parameter being passed must be a an anonymous function.

    But you are not passing an anonymous function. You are calling an anonymous function and passing the result.

    You could pass a function (rather than the result of calling a function), and then within aFunction(x) you’d test that x is a function and then call it to get the value/object to work with:

    function aFunction(x) {
        if(typeof x === "function") {
            var workingValue = x();
        }
    }
    

    But of course that doesn’t in any way ensure that the function passed in will return a value in the right format. Also it doesn’t ensure that it is an anonymous function.

    Your number 2 syntax is fine as is, with the advantage of keeping any working variables out of the current scope (assuming that doesn’t break something), but as far as your stated goal of making sure you don’t forget to call aFunction() at the end of your other processing, well… My opinion is it just makes the code harder for others to read.

    EDIT: Just saw your updated question, where you’ve decided to do what I mentioned above (seems you were updating at the same time I was answering). Seriously, I really think this is a bad design pattern, and I only really mentioned it as a hypothetical solution – I don’t think I made it clear initially that I don’t actually recommend it.

    In my opinion your original number 1 version is the best way, or if you must use a function your number 2 version is OK.

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

Sidebar

Related Questions

I have the following jQuery code: $('.save').submit(function(e){ var formElement = $(this); var data =
I have the following code snippet: $.getJSON(http://localhost:8080/images, function(data) { var items = []; $.each(data,
I have the following simple code: var tabs = null; function addTab(id, title, data)
I have the following code: var items = db.Name.Where(x => x.Forename.IndexOf(john, StringComparison.OrdinalIgnoreCase) >= 0).Take(20);
I have the following code: var foo = (from data in pivotedData.AsEnumerable() select new
I have following code that I creating a grid var store = Ext.create('Ext.data.ArrayStore', {
I have the following code: var tempIdx = document.getElementById('cityBuild').selectedIndex; var tempBuilding = document.getElementById('cityBuild').options[tempIdx].value; //
I have the following code: var sl: THashedStringList; begin sl:= THashedStringList.Create; sl.Duplicates := dupIgnore;
I am using ASP.NET 3.5 with iTextSharp and I have the following code: var
How do I access user preferences in Firefox? I have the following code: var

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.