JavaScript,继承的函数被打印为未定义
我想知道为什么JavaScript,继承的函数被打印为未定义,javascript,json,oop,stringify,Javascript,Json,Oop,Stringify,我想知道为什么JSON.stringify(this.Master.Func)返回'undefined'而不是function(){…} 函数本身通过添加()来执行 JSfiddle: 代码 var $ = {}; // some global var Master = { property: 'Property', Func: function() { console.log('I am Func insid
JSON.stringify(this.Master.Func)
返回'undefined'而不是function(){…}
函数本身通过添加()
来执行
JSfiddle:
代码
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();
如果您想查看函数文本,只需像这样调用
toString
方法
console.log(this.Master.Func.toString());
try(新从属)的可能重复项。ShowFunc()代码>