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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:16:51+00:00 2026-06-09T08:16:51+00:00

I have a simple object called Entry , which until just recently only had

  • 0

I have a simple object called Entry, which until just recently only had a few basic properties. During certain events, I would perform REST actions on these objects to an API living in C# code. Until just recently, this worked fine. I would POST or PUT these objects in the body of an AJAX request using my little request service, which ended like so:

$.ajax(url, 
{
    type:method,
    headers:headers,
    data:options.data, //there is an instance of Entry in options.data
    timeout: this.timeoutLength
});

Recently I added a function to the Entry.

function Entry(foo, bar){
    this.foo = foo;
    this.bar = bar;
    this.doStuff = function(){ ... } //added this code
}

I added the function, but questioned the user story behind its use again so I switched back to the browser, refreshed, and ran some more tests. Without ever calling the function doStuff anywhere, it’s effects were being seen.

I replaced doStuff‘s body with a simple alert, and sure enough every time I did something that behind the scenes called an AJAX request with an Entry object, I got my alert message. Why in the world is this happening? Why is doStuff being called without me telling it to?

  • 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-09T08:16:59+00:00Added an answer on June 9, 2026 at 8:16 am

    Edit: tldr; Ajax calls any functions in an object when serializing it. If you care why, read on:

    This drove me a little crazy, until I managed to follow the stack trace backwards. It seems the function doStuff was being called by jquery-1.7.1.js $.ajax. But why?

    The object in question, "entry" here, has to be serialized into a query string for sending over the network. In doing so, it goes through each of the object’s properties.

    First, (on line 7610 for me) it checks if the object isPlainObject. It may have been before, but having a function no longer makes it so. Instead, it does the following:

    jQuery.each( a, function() {
        add( this.name, this.value );
    });
    

    It defined add just a little earlier, as so:

    add = function( key, value ) {
        // If value is a function, invoke it and return its value
        value = jQuery.isFunction( value ) ? value() : value;
        s[ s.length ] = encodeURIComponent( key ) + "=" + encodeURIComponent( value );
    };
    

    Note the comment jQuery lovingly provided us. It goes through every property of the object, and if one is a function it uses the function’s return value as the "value" of the "key/value" pair. It was calling doStuff here to get a value (which of course returned undefined because the function simply called an alert)

    I will add, for my own sake, that the documentation doesn’t really make this explicit. I always assumed it ignored functions, but its good to not that it most certainly does not.

    data (Object, String)

    Data to be sent to the server. It is converted to a query string, if not already a string. It’s appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional setting (described below).

    Finally, through a simple test (not full reading the code), it seems that JSON.stringify does ignore functions altogether.

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

Sidebar

Related Questions

I have an simple custom object called MyObject (a couple of basic properties and
This should be a simple one: I have an observableArray object called To in
I have a simple object that allows you to assign three properties (x,y,z) (lets
Say I have a simple object which supports implicit casting to System.String public sealed
Currently I have a simple knockoutJS object, with a few observables. But this object
I have a simple JavaScript Array object containing a few numbers. [267, 306, 108]
I have huge amont of geographic data represented in simple object structure consisting only
I have a very simple object called CellData. Is defined as: public sealed class
I have a simple object, of type ObjectX, with a simple method called doSomething().
I have quite a simple situation. I've got 3 objects: an object called TrainingConsultant

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.