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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:16:38+00:00 2026-05-24T10:16:38+00:00

This is the source of $.parseJSON function (data) { if (typeof data !== string

  • 0

This is the source of $.parseJSON

function (data) {
    if (typeof data !== "string" || !data) {
        return null;
    }

    // Make sure leading/trailing whitespace is removed (IE can't handle it)
    data = jQuery.trim(data);

    // Attempt to parse using the native JSON parser first
    if (window.JSON && window.JSON.parse) {
        return window.JSON.parse(data);
    }

    // Make sure the incoming data is actual JSON
    // Logic borrowed from http://json.org/json2.js
    if (rvalidchars.test(data.replace(rvalidescape, "@").replace(rvalidtokens, "]").replace(rvalidbraces, ""))) {

        return (new Function("return " + data))();

    }
    jQuery.error("Invalid JSON: " + data);
}

I have trouble understanding the following fallbacks

return (new Function("return " + data))();

and also ( this one is not in jQuery )

return (eval('('+ data + ')')

I would like to know these things

  1. How this parsing fallback works really?
  2. Why eval is not used in the fallback? (Is it not faster than new Function())
  • 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-24T10:16:39+00:00Added an answer on May 24, 2026 at 10:16 am

    new Function() allows you to pass your function as a string.

    In this case, the function is created to simply return the object described by the json string. Since the json is a valid object literal, this function simply returns the object defined in the json. The new function is immediately invoked, returning that object.

    As far as performance, some quick googling found claims that new Function() is faster than eval, though I have not tested this myself.

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

Sidebar

Related Questions

this thing almost works: function myClass(url) { this.source = url; this.rq = null; this.someOtherProperty
I'm working this source code: #include <string> #include <vector> #include <iostream> #include <istream> #include
//This source is a line read from a file String src = 23570006,music,**,wu(),1,exam,\Monday9,10(H2-301)\,1-10,score,; //This
I have several sources of tables with personal data, like this: SOURCE 1 ID,
I'm trying this source where jqGrid gets some json data from django: link Unfortunately
Consider this source: public partial class FormTest : Form { private Test test {
like this (source: photoshopessentials.com )
I have this source code from 2001 that I would like to compile. It
I'm using a MediaElement with this source: http://origin-community.ministryofsound.com/asx/radio/mosRadio.asx When I open it with Windows
So we have a photo like this (source: vkontakte.ru ) How to detect that

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.