我正在开发一个由许多对象和函数(对象方法)组成的javascript应用程序.我希望能够在应用程序的生命周期中记录许多事件.我的问题是在记录器内部我想知道哪个函数调用了日志条目,所以我可以将这些数据与日志消息一起保存.这意味着每个函数都需要能够以某种方式引用自身,因此我可以将该引用传递给记录器.我正在使用javascript严格模式,因此arguments.callee不允许在函数内部使用.
这是一个可以运行的非常简化的代码示例.alert为了简单起见,我只是在这里使用而不是我的记录器.
(function(){
"use strict";
window.myObject = {
id : 'myObject',
myFunc : function(){
alert(this.id); // myObject
alert(this.myFunc.id); // myFunc - I don't want to do this. I want something generic for all functions under any object
alert('???') // myFunc
alert(arguments.callee.id); // Will throw an error because arguments.callee in not allowed in strict mode
}
}
myObject.myFunc.id = 'myFunc';
myObject.myFunc();
})();
Run Code Online (Sandbox Code Playgroud)
this相关myObject和不相关myFunc我正在寻找一个转储多维数组的函数,以便输出是有效的PHP代码.
假设我有以下数组:
$person = array();
$person['first'] = 'Joe';
$person['last'] = 'Smith';
$person['siblings'] = array('Jane' => 'sister', 'Dan' => 'brother', 'Paul' => 'brother');
Run Code Online (Sandbox Code Playgroud)
现在我想转储$ person变量,这样转储字符串输出,如果被解析,将是有效的PHP代码,重新定义$ person变量.
所以做以下事情:
dump_as_php($person);
Run Code Online (Sandbox Code Playgroud)
将输出:
$person = array(
'first' => 'Joe',
'last' => 'Smith',
'siblings' => array(
'Jane' => 'sister',
'Dan' => 'brother',
'Paul' => 'brother'
)
);
Run Code Online (Sandbox Code Playgroud)