Тема: JS: Неявное наследование объекта?
При обходе всех объектов приходится вводить ограничение.
function ObjWalker(obj,count,name){
var result = "";
count = count + 1;
// Ограничение итераций, иначе попытается скушать всю память или не будет работать - в зависимости от среды запуска.
if(count > 3) return name+": "+obj+" is limited ["+typeof obj+"]\n";
if(null == obj) return name+"."+obj+" = null ["+typeof obj+"]\n";
var tmp = null;
for (var ch in obj){
tmp = obj[ch];
if( typeof tmp != "object" ){
result += name+"."+ch+" = "+tmp+" ["+typeof tmp+"]\n";
}else{
result += name+"."+ch+" = "+tmp+" ["+typeof tmp+"]\n";
result += ObjWalker(tmp,count,name+"."+ch.toString());
}
tmp = null;
}
if("" == result){
return name+" "+obj+" is empty ["+typeof obj+"] \n"
}
return result;
}
var pocient1 = new Object();
var pocient = this;
var result = "this:\n"+ObjWalker(pocient,0,"this")+"\n===========\n";
WScript.Echo(result);
Вот что выводится в результате:
>cscript selfinfo.js
Сервер сценариев Windows (Microsoft ®) версия 5.8
© Корпорация Майкрософт (Microsoft Corp.), 1996-2001. Все права защищены.this:
this.WScript = Windows Script Host [object]
this.WScript Windows Script Host is empty [object]
this.WSH = Windows Script Host [object]
this.WSH Windows Script Host is empty [object]
this.ObjWalker = function ObjWalker(obj,count,name){
var result = "";
count = count + 1;
// Ограничение итераций, иначе попытается скушать всю память или не будет работать - в зависимости от среды запуска.
if(count > 3) return name+": "+obj+" is limited ["+typeof obj+"]\n";
if(null == obj) return name+"."+obj+" = null ["+typeof obj+"]\n";
var tmp = null;
for (var ch in obj){
tmp = obj[ch];
if( typeof tmp != "object" ){
result += name+"."+ch+" = "+tmp+" ["+typeof tmp+"]\n";
}else{
result += name+"."+ch+" = "+tmp+" ["+typeof tmp+"]\n";
result += ObjWalker(tmp,count,name+"."+ch.toString());
}
tmp = null;
}
if("" == result){
return name+" "+obj+" is empty ["+typeof obj+"] \n"
}
return result;
} [function]
this.pocient1 = [object Object] [object]
this.pocient1 [object Object] is empty [object]
this.pocient = [object]
this.pocient.WScript = Windows Script Host [object]
this.pocient.WScript Windows Script Host is empty [object]
this.pocient.WSH = Windows Script Host [object]
this.pocient.WSH Windows Script Host is empty [object]
this.pocient.ObjWalker = function ObjWalker(obj,count,name){
var result = "";
count = count + 1;
// Ограничение итераций, иначе попытается скушать всю память или не будет работать - в зависимости от среды запуска.
if(count > 3) return name+": "+obj+" is limited ["+typeof obj+"]\n";
if(null == obj) return name+"."+obj+" = null ["+typeof obj+"]\n";
var tmp = null;
for (var ch in obj){
tmp = obj[ch];
if( typeof tmp != "object" ){
result += name+"."+ch+" = "+tmp+" ["+typeof tmp+"]\n";
}else{
result += name+"."+ch+" = "+tmp+" ["+typeof tmp+"]\n";
result += ObjWalker(tmp,count,name+"."+ch.toString());
}
tmp = null;
}
if("" == result){
return name+" "+obj+" is empty ["+typeof obj+"] \n"
}
return result;
} [function]
this.pocient.pocient1 = [object Object] [object]
this.pocient.pocient1 [object Object] is empty [object]
this.pocient.pocient = [object]
this.pocient.pocient.WScript = Windows Script Host [object]
this.pocient.pocient.WScript: Windows Script Host is limited [object]
this.pocient.pocient.WSH = Windows Script Host [object]
this.pocient.pocient.WSH: Windows Script Host is limited [object]
this.pocient.pocient.ObjWalker = function ObjWalker(obj,count,name){
var result = "";
count = count + 1;
// Ограничение итераций, иначе попытается скушать всю память или не будет работать - в зависимости от среды запуска.
if(count > 3) return name+": "+obj+" is limited ["+typeof obj+"]\n";
if(null == obj) return name+"."+obj+" = null ["+typeof obj+"]\n";
var tmp = null;
for (var ch in obj){
tmp = obj[ch];
if( typeof tmp != "object" ){
result += name+"."+ch+" = "+tmp+" ["+typeof tmp+"]\n";
}else{
result += name+"."+ch+" = "+tmp+" ["+typeof tmp+"]\n";
result += ObjWalker(tmp,count,name+"."+ch.toString());
}
tmp = null;
}
if("" == result){
return name+" "+obj+" is empty ["+typeof obj+"] \n"
}
return result;
} [function]
this.pocient.pocient.pocient1 = [object Object] [object]
this.pocient.pocient.pocient1: [object Object] is limited [object]
this.pocient.pocient.pocient = [object]
this.pocient.pocient.pocient: is limited [object]
this.pocient.pocient.result = undefined [undefined]
this.pocient.result = undefined [undefined]
this.result = undefined [undefined]===========
Можно ли сделать обход, так чтоб не требовалось вводить ограничение?
Помогите определить в каком месте происходит неявное создание наследника, у меня две версии:
1. Вот тут: for (var ch in obj){
2. Или тут при рекурсивном вызове: result += ObjWalker(tmp,count,name+"."+i.toString());

