我需要通过ajax json接收一些混合对象并将它们放入Global_obj中 我不能使用$ .extend(),因为有些项目是javascript函数。
有人知道另一种(更好)的方法吗?
<?php
$ajax_response = [
"Obj_1" => [
"key1_1" => "function(){console.log('ok key1_1')}",
"key1_2" => "<div>Something key1_2</div>"
],
"Obj_2" => [
"key2_1" => "function(){console.log('ok key2_1')}",
"key2_2" => "<div>Something key2_2</div>"
]
];
&GT;
<script>
var Global_obj = {};
var ajax_response = <?php echo json_encode($ajax_response); ?>;
$.each(ajax_response,function(key){
Global_obj[key] = {};
$.each(this,function(subkey){
if(this.indexOf('function') > -1){
Global_obj[key][subkey] = eval( '('+this+ ')' );
}else{
Global_obj[key][subkey] = ''+this;
}
});
});
Global_obj.Obj_1.key1_1(); // show "ok key1_1"
已编辑:我已编辑了mi old question,因为标记为重复
所以编辑过的问题是: 有人知道另一种(更好)的方法吗?
由于