Is it possible to access JSON properties with relative syntax when using JSON defined functions?

Posted by Justin Vincent on Stack Overflow See other posts from Stack Overflow or by Justin Vincent
Published on 2010-05-05T21:36:39Z Indexed on 2010/05/05 21:38 UTC
Read the original article Hit count: 232

Filed under:
|
|
// 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?

© Stack Overflow or respective owner

Related posts about JSON

Related posts about this