Is it possible to access JSON properties with relative syntax when using JSON defined functions?
- by Justin Vincent
// 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?