<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[Серый форум &mdash; JS: Неявное наследование объекта?]]></title>
		<link>https://forum.script-coding.com/viewtopic.php?id=9446</link>
		<atom:link href="https://forum.script-coding.com/extern.php?action=feed&amp;tid=9446&amp;type=rss" rel="self" type="application/rss+xml" />
		<description><![CDATA[Недавние сообщения в теме «JS: Неявное наследование объекта?».]]></description>
		<lastBuildDate>Fri, 04 Apr 2014 09:17:56 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[Re: JS: Неявное наследование объекта?]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=81481#p81481</link>
			<description><![CDATA[<p>Спасибо!<br /></p><div class="quotebox"><cite>omegastripes пишет:</cite><blockquote><p>Что можно представить как:<br /></p><div class="codebox"><pre><code>this.pocient = this;</code></pre></div><p>Иными словами, одно из свойств глобального объекта ссылается на самого себя.</p></blockquote></div><p>Действительно, если первый вызов заменить на<br /></p><div class="codebox"><pre><code>var result  = &quot;this:\n&quot;+ObjWalker(this,0,&quot;this&quot;)+&quot;\n===========\n&quot;;</code></pre></div><p>То циклическая ссылка не появляется.<br />Однако:<br />В HTA можно обратиться к объекту <span style="color: red">так</span> или <span style="color: green">так</span>:<br /></p><div class="quotebox"><blockquote><p>&lt;HTML&gt;<br />&lt;HEAD&gt;<br />&lt;HTA:APPLICATION ID=&quot;oHTA&quot;/&gt;<br />&lt;SCRIPT&gt;<br />&nbsp; &nbsp; window.onload = function(){<br />&nbsp; &nbsp; &nbsp; &nbsp; document.body.innerHTML = &quot;&lt;PRE id=oPre&gt;&quot;+<br /><span style="color: red">oHTA.attributes.onclick.ownerDocument.documentElement.firstChild.children.<br />oHTA.attributes.onclick.ownerDocument.documentElement.firstChild.children.<br />oHTA.commandLine</span>+&quot;\n&quot;+<br /><span style="color: green">oHTA.commandLine</span>+&quot;&lt;/PRE&gt;&quot;;<br />&nbsp; &nbsp; }<br />&lt;/SCRIPT&gt;<br />&lt;/HEAD&gt;</p></blockquote></div><p>Изначально этот факт ввел меня в заблуждение.</p>]]></description>
			<author><![CDATA[null@example.com (-red-)]]></author>
			<pubDate>Fri, 04 Apr 2014 09:17:56 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=81481#p81481</guid>
		</item>
		<item>
			<title><![CDATA[Re: JS: Неявное наследование объекта?]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=81427#p81427</link>
			<description><![CDATA[<div class="quotebox"><cite>-red- пишет:</cite><blockquote><p>в каком месте происходит неявное создание наследника</p></blockquote></div><p><strong>-red-</strong>, я бы предпочел термин <em>циклическая ссылка (iterative reference)</em> или что-то в этом роде, поскольку, на мой взгляд, у Вас в коде всё происходит явно:<br /></p><div class="codebox"><pre><code>var pocient = this;</code></pre></div><p>Что можно представить как:<br /></p><div class="codebox"><pre><code>this.pocient = this;</code></pre></div><p>Иными словами, одно из свойств глобального объекта ссылается на самого себя. Для того, чтобы в данном случае избежать циклической ссылки, достаточно добавить проверку является ли свойство объекта самим объектом:<br /></p><div class="codebox"><pre><code>function ObjWalker(obj,count,name){
    var result = &quot;&quot;;
    count = count + 1;
    // Ограничение итераций, иначе попытается скушать всю память или не будет работать - в зависимости от среды запуска.
    // if(count &gt; 3) return name+&quot;: &quot;+obj+&quot; is limited [&quot;+typeof obj+&quot;]\n&quot;;
    if(null == obj) return name+&quot;.&quot;+obj+&quot; = null [&quot;+typeof obj+&quot;]\n&quot;;
    var tmp = null;
    for (var ch in obj){
        tmp = obj[ch];
        if( typeof tmp != &quot;object&quot; ){
            result += name+&quot;.&quot;+ch+&quot; = &quot;+tmp+&quot; [&quot;+typeof tmp+&quot;]\n&quot;;
        }else{
            result += name+&quot;.&quot;+ch+&quot; = &quot;+tmp+&quot; [&quot;+typeof tmp+&quot;]\n&quot;;
            // дополнительная проверка
            if(obj !== tmp){ 
                result += ObjWalker(tmp,count,name+&quot;.&quot;+ch.toString());
            }else{
                result += &quot;(iterative reference)\n&quot;;
            }
        tmp = null;
        }
    }
    if(&quot;&quot; == result){
        return name+&quot; &quot;+obj+&quot; is empty [&quot;+typeof obj+&quot;] \n&quot;
    }
    return result;
}
var pocient1 = new Object();
var pocient = this;
var result  = &quot;this:\n&quot;+ObjWalker(pocient,0,&quot;this&quot;)+&quot;\n===========\n&quot;;
WScript.Echo(result);</code></pre></div><p>Конечно, для более сложных циклических ссылок, состоящих из многих звеньев (как в примере <strong>Rumata</strong>), необходим более мощный и гибкий алгоритм проверки на цикличность. Оптимальным решением была бы запись всей цепочки &quot;родословной&quot; для каждого анализируемого объекта, и тогда в случае циклической ссылки любой сложности, анализ этой цепочки позволил бы выявить 2 последовательных повторяющихся отрезка - второй отрезок должен отсекаться, заменяться сообщением о начале циклической ссылки.</p>]]></description>
			<author><![CDATA[null@example.com (omegastripes)]]></author>
			<pubDate>Wed, 02 Apr 2014 16:42:22 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=81427#p81427</guid>
		</item>
		<item>
			<title><![CDATA[Re: JS: Неявное наследование объекта?]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=81423#p81423</link>
			<description><![CDATA[<p>Что значит &quot;неявное наследование&quot; в данном случае? Может быть Вы имеете в виду циклические ссылки объектов друг на друга. Например:<br /></p><div class="codebox"><pre><code>
var x = {};
var y = {};

x.y = y;
y.x = x;

var z = {};
z.z = z;
</code></pre></div><div class="quotebox"><blockquote><p>Можно ли сделать обход, так чтоб не требовалось вводить ограничение?</p></blockquote></div><p>Теоретически - да. В том же jQuery.extend как-то делается клонирование объектов с циклическими ссылками. Задача для меня не особо актуальна, поэтому я и не пытаюсь разобраться, как это реализовано там.</p><p>Для себя я решил отслеживать явные ссылки &quot;на себя&quot; (как в z) и ограничивать некоторой глубиной вложенности по умолчанию, которую можно переопределять явно.</p>]]></description>
			<author><![CDATA[null@example.com (Rumata)]]></author>
			<pubDate>Wed, 02 Apr 2014 11:30:44 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=81423#p81423</guid>
		</item>
		<item>
			<title><![CDATA[JS: Неявное наследование объекта?]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=81420#p81420</link>
			<description><![CDATA[<p>При обходе всех объектов приходится вводить ограничение.<br /></p><div class="codebox"><pre><code>
function ObjWalker(obj,count,name){
    var result = &quot;&quot;;
    count = count + 1;
    // Ограничение итераций, иначе попытается скушать всю память или не будет работать - в зависимости от среды запуска.
    if(count &gt; 3) return name+&quot;: &quot;+obj+&quot; is limited [&quot;+typeof obj+&quot;]\n&quot;;
    if(null == obj) return name+&quot;.&quot;+obj+&quot; = null [&quot;+typeof obj+&quot;]\n&quot;;
    var tmp = null;
    for (var ch in obj){
        tmp = obj[ch];
        if( typeof tmp != &quot;object&quot; ){
            result += name+&quot;.&quot;+ch+&quot; = &quot;+tmp+&quot; [&quot;+typeof tmp+&quot;]\n&quot;;
        }else{
            result += name+&quot;.&quot;+ch+&quot; = &quot;+tmp+&quot; [&quot;+typeof tmp+&quot;]\n&quot;;
            result += ObjWalker(tmp,count,name+&quot;.&quot;+ch.toString());
        }
        tmp = null;
    }
    if(&quot;&quot; == result){
        return name+&quot; &quot;+obj+&quot; is empty [&quot;+typeof obj+&quot;] \n&quot;
    }
    return result;
}
var pocient1 = new Object();
var pocient = this;
var result  = &quot;this:\n&quot;+ObjWalker(pocient,0,&quot;this&quot;)+&quot;\n===========\n&quot;;
WScript.Echo(result);
</code></pre></div><p>Вот что выводится в результате:<br /></p><div class="quotebox"><blockquote><p>&gt;cscript selfinfo.js<br />Сервер сценариев Windows (Microsoft ®) версия 5.8<br />© Корпорация Майкрософт (Microsoft Corp.), 1996-2001. Все права защищены.</p><p>this:<br />this.WScript = Windows Script Host [object]<br />this.WScript Windows Script Host is empty [object]<br />this.WSH = Windows Script Host [object]<br />this.WSH Windows Script Host is empty [object]<br />this.ObjWalker = function ObjWalker(obj,count,name){<br />&nbsp; &nbsp; &nbsp; &nbsp; var result = &quot;&quot;;<br />&nbsp; &nbsp; &nbsp; &nbsp; count = count + 1;<br />&nbsp; &nbsp; &nbsp; &nbsp; // Ограничение итераций, иначе попытается скушать всю память или не будет работать - в зависимости от среды запуска.<br />&nbsp; &nbsp; &nbsp; &nbsp; if(count &gt; 3) return name+&quot;: &quot;+obj+&quot; is limited [&quot;+typeof obj+&quot;]\n&quot;;<br />&nbsp; &nbsp; &nbsp; &nbsp; if(null == obj) return name+&quot;.&quot;+obj+&quot; = null [&quot;+typeof obj+&quot;]\n&quot;;<br />&nbsp; &nbsp; &nbsp; &nbsp; var tmp = null;<br />&nbsp; &nbsp; &nbsp; &nbsp; for (var ch in obj){<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tmp = obj[ch];<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if( typeof tmp != &quot;object&quot; ){<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result += name+&quot;.&quot;+ch+&quot; = &quot;+tmp+&quot; [&quot;+typeof tmp+&quot;]\n&quot;;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else{<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result += name+&quot;.&quot;+ch+&quot; = &quot;+tmp+&quot; [&quot;+typeof tmp+&quot;]\n&quot;;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result += ObjWalker(tmp,count,name+&quot;.&quot;+ch.toString());<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tmp = null;<br />&nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; &nbsp; &nbsp; if(&quot;&quot; == result){<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return name+&quot; &quot;+obj+&quot; is empty [&quot;+typeof obj+&quot;] \n&quot;<br />&nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; &nbsp; &nbsp; return result;<br />} [function]<br />this.pocient1 = [object Object] [object]<br />this.pocient1 [object Object] is empty [object]<br />this.pocient =&nbsp; [object]<br />this.pocient.WScript = Windows Script Host [object]<br />this.pocient.WScript Windows Script Host is empty [object]<br />this.pocient.WSH = Windows Script Host [object]<br />this.pocient.WSH Windows Script Host is empty [object]<br />this.pocient.ObjWalker = function ObjWalker(obj,count,name){<br />&nbsp; &nbsp; &nbsp; &nbsp; var result = &quot;&quot;;<br />&nbsp; &nbsp; &nbsp; &nbsp; count = count + 1;<br />&nbsp; &nbsp; &nbsp; &nbsp; // Ограничение итераций, иначе попытается скушать всю память или не будет работать - в зависимости от среды запуска.<br />&nbsp; &nbsp; &nbsp; &nbsp; if(count &gt; 3) return name+&quot;: &quot;+obj+&quot; is limited [&quot;+typeof obj+&quot;]\n&quot;;<br />&nbsp; &nbsp; &nbsp; &nbsp; if(null == obj) return name+&quot;.&quot;+obj+&quot; = null [&quot;+typeof obj+&quot;]\n&quot;;<br />&nbsp; &nbsp; &nbsp; &nbsp; var tmp = null;<br />&nbsp; &nbsp; &nbsp; &nbsp; for (var ch in obj){<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tmp = obj[ch];<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if( typeof tmp != &quot;object&quot; ){<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result += name+&quot;.&quot;+ch+&quot; = &quot;+tmp+&quot; [&quot;+typeof tmp+&quot;]\n&quot;;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else{<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result += name+&quot;.&quot;+ch+&quot; = &quot;+tmp+&quot; [&quot;+typeof tmp+&quot;]\n&quot;;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result += ObjWalker(tmp,count,name+&quot;.&quot;+ch.toString());<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tmp = null;<br />&nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; &nbsp; &nbsp; if(&quot;&quot; == result){<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return name+&quot; &quot;+obj+&quot; is empty [&quot;+typeof obj+&quot;] \n&quot;<br />&nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; &nbsp; &nbsp; return result;<br />} [function]<br />this.pocient.pocient1 = [object Object] [object]<br />this.pocient.pocient1 [object Object] is empty [object]<br />this.pocient.pocient =&nbsp; [object]<br />this.pocient.pocient.WScript = Windows Script Host [object]<br />this.pocient.pocient.WScript: Windows Script Host is limited [object]<br />this.pocient.pocient.WSH = Windows Script Host [object]<br />this.pocient.pocient.WSH: Windows Script Host is limited [object]<br />this.pocient.pocient.ObjWalker = function ObjWalker(obj,count,name){<br />&nbsp; &nbsp; &nbsp; &nbsp; var result = &quot;&quot;;<br />&nbsp; &nbsp; &nbsp; &nbsp; count = count + 1;<br />&nbsp; &nbsp; &nbsp; &nbsp; // Ограничение итераций, иначе попытается скушать всю память или не будет работать - в зависимости от среды запуска.<br />&nbsp; &nbsp; &nbsp; &nbsp; if(count &gt; 3) return name+&quot;: &quot;+obj+&quot; is limited [&quot;+typeof obj+&quot;]\n&quot;;<br />&nbsp; &nbsp; &nbsp; &nbsp; if(null == obj) return name+&quot;.&quot;+obj+&quot; = null [&quot;+typeof obj+&quot;]\n&quot;;<br />&nbsp; &nbsp; &nbsp; &nbsp; var tmp = null;<br />&nbsp; &nbsp; &nbsp; &nbsp; for (var ch in obj){<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tmp = obj[ch];<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if( typeof tmp != &quot;object&quot; ){<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result += name+&quot;.&quot;+ch+&quot; = &quot;+tmp+&quot; [&quot;+typeof tmp+&quot;]\n&quot;;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else{<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result += name+&quot;.&quot;+ch+&quot; = &quot;+tmp+&quot; [&quot;+typeof tmp+&quot;]\n&quot;;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result += ObjWalker(tmp,count,name+&quot;.&quot;+ch.toString());<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tmp = null;<br />&nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; &nbsp; &nbsp; if(&quot;&quot; == result){<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return name+&quot; &quot;+obj+&quot; is empty [&quot;+typeof obj+&quot;] \n&quot;<br />&nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; &nbsp; &nbsp; return result;<br />} [function]<br />this.pocient.pocient.pocient1 = [object Object] [object]<br />this.pocient.pocient.pocient1: [object Object] is limited [object]<br />this.pocient.pocient.pocient =&nbsp; [object]<br />this.pocient.pocient.pocient:&nbsp; is limited [object]<br />this.pocient.pocient.result = undefined [undefined]<br />this.pocient.result = undefined [undefined]<br />this.result = undefined [undefined]</p><p>===========</p></blockquote></div><p>Можно ли сделать обход, так чтоб не требовалось вводить ограничение?<br />Помогите определить в каком месте происходит неявное создание наследника, у меня две версии:<br />1. Вот тут: for (var ch in obj){<br />2. Или тут при рекурсивном вызове: result += ObjWalker(tmp,count,name+&quot;.&quot;+i.toString());</p>]]></description>
			<author><![CDATA[null@example.com (-red-)]]></author>
			<pubDate>Wed, 02 Apr 2014 07:20:30 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=81420#p81420</guid>
		</item>
	</channel>
</rss>
