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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:36:26+00:00 2026-05-14T21:36:26+00:00

// JavaScript JSON var myCode = { message : Hello World, helloWorld : function()

  • 0
// JavaScript JSON
var myCode = 
{
   message : "Hello World",

   helloWorld : function()
   {
     alert(this.message);
   }
};
myCode.helloWorld();

The above JavaScript code will alert ‘undefined’.

To make it work for real the code would need to look like the following… (note the literal path to myCode.message)

// JavaScript JSON
var myCode = 
{
   message : "Hello World",

   helloWorld : function()
   {
     alert(myCode.message);
   }
};
myCode.helloWorld();

My question is… if I declare functions using json in this way, is there some “relative” way to get access to myCode.message or is it only possible to do so using the literal namespace path myCode.message?

  • 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-14T21:36:27+00:00Added an answer on May 14, 2026 at 9:36 pm

    Your first example works, the this value inside the helloWorld function will refer to the myCode object itself, because you invoked it by myCode.helloWorld();

    When you invoke a function that is member of an object, this object will be set as the this value of the function.

    In this case myCode is the base object of the myCode.helloWorld reference.

    There are two more cases about how the this keyword is implicitly, for example, when you call a function that is not bound as property of any object, i.e.:

    myFunc();
    

    The this value inside myFunc will point to the global object.

    When you use the new operator:

    var obj = new MyFunc();
    

    The this value inside MyFunc will refer to a newly created object.

    And you can set the this value of a function explicitly, using call or apply:

    function test () {
      return this + " World";
    }
    test.call("Hello"); // "Hello World"
    

    Just a note, that’s not JSON, JSON is simply a data interchange format, its grammar differs with the JavaScript Object Literal syntax, for example:

    { foo: "bar" }
    

    The above is a valid JavaScript object literal, but is not valid JSON, JSON requires the property identifiers to be wrapped between quotes, and it has a limited set of data types allowed, for example, you cannot have functions as members of a JSON object.

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

Sidebar

Related Questions

JavaScript/JQuery var arr=[]; $('.element').each(function(i) { arr.push({id:i,value:$(this).attr('data-value')}); }); $.get('/json.php?arr='+arr,function(result) { alert(result); }); PHP <?php $j
I have updated my jSon object using the code <script type=text/javascript > jQuery(document).ready(function($){ var
How to get the values from JSON in javascript following is my code var
Possible Duplicate: How do I test for an empty Javascript object from JSON? var
<script type=text/javascript> $(document).ready(function() { $('input#zip').autocomplete({ var cityAndState = joinedValues.split(|)[1]; dataType: json, source: ../src/php/registration/getFanLoc.php, minLength:
I am studying javascript and json but every line of code I write is
Here's my code: $(#ddlCiudad).change(function () { var idCity = $(#ddlCiudad).val(); $.getJSON(/ProductCheckout/GetPriceForLocation, { cityId: idCity,
In my javascript, I have: var testdate = {'TheNewDate' : '12/02/2011'}; $(#mydiv).click(function () {
I'm trying to parse JSON in JavaScript, but I'm getting this error in Rails:
Here is my code: jQuery.getJSON('http://twitter.com/status/user_timeline/brianj_smith.json?count=1&callback=?', function(data){ if (data.length > 0) { var link =

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.