开发者

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

开发者_StackOverflow// 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?


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.


No.

First of all, this is not JSON. What you have here is just an object literal. JSON is a data format, and doesn't support functions.

Second, is an understanding of how this binding works.

Unless explicitly bound via call() or apply() or used inside the method of an instantiated object, this always refers to the current window object.

Since you define myCode as an object literal, the "instantiated object" case doesn't apply. Also, you aren't using call() or apply() so those don't apply either. So in the code of your first example, this is equal to window which is why you get undefined (since window.message is undefined).

You already know one way around this - your 2nd code snippet. Your other choice is to actually go ahead and use call() or apply().

var myCode = 
{
   message : "Hello World",

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

This way, you are basically telling Javascript to use myCode as this and it will work as expected.

Another option is to make myCode instantiable.

var myCode = function()
{
  this.message = 'Hello World';
  this.helloWorld = function()
  {
    alert( this.message );
  }
}
var mc = new myCode();
mc.helloWorld();


CMS hit the nail on the head, if you want more info you could check out http://www.scottlogic.co.uk/blog/chris/2010/05/what-is-this/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜