0

I am wondering why JSON.stringify(this.Master.Func) returns 'undefined' instead of function() { ... }.

Function itself executes by adding ().

JSfiddle: http://jsfiddle.net/t4ngY/

CODE

var $ = {}; // some global

var Master = 
    {
        property: 'Property',

        Func: function()
        {
            console.log('I am Func inside Master');
        },

        PassToGlobal: function()
        {
            $.master = this;
        }
    };

Master.PassToGlobal();

var Slave =
    {
        Master: $.master,

        ShowFunc: function()
        {
            console.log(JSON.stringify(this.Master.Func)); //returns undef
            this.Master.Func(); //prints `I am Func inside Master`
        }
    }

Slave.ShowFunc();
Mike
  • 842
  • 1
  • 9
  • 31
  • 4
    possible duplicate of [Is there any possibility to have JSON.stringify preserve functions?](http://stackoverflow.com/questions/7759200/is-there-any-possibility-to-have-json-stringify-preserve-functions) – kalley Nov 10 '13 at 20:23
  • try `(new Slave).ShowFunc();` – Protomen Nov 10 '13 at 20:43

1 Answers1

0

if you want see function text you can simply call toString method like this

console.log(this.Master.Func.toString()); 
Grundy
  • 13,356
  • 3
  • 35
  • 55