<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>mFabrik - mobile sites, apps, HTML5 and CMS software development &#187; mootools</title>
	<atom:link href="http://blog.mfabrik.com/tag/mootools/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.mfabrik.com</link>
	<description>Freedom delivered.</description>
	<lastBuildDate>Wed, 03 Aug 2011 09:47:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>MooTools setOptions() nullifies object references</title>
		<link>http://blog.mfabrik.com/2008/08/19/mootools-setoptions-nullifies-object-references/</link>
		<comments>http://blog.mfabrik.com/2008/08/19/mootools-setoptions-nullifies-object-references/#comments</comments>
		<pubDate>Tue, 19 Aug 2008 00:21:08 +0000</pubDate>
		<dc:creator>Tuukka Mustonen</dc:creator>
				<category><![CDATA[debugging]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[mootools]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[solution]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[addressing]]></category>
		<category><![CDATA[merge]]></category>
		<category><![CDATA[object]]></category>
		<category><![CDATA[problem]]></category>
		<category><![CDATA[reference]]></category>
		<category><![CDATA[setOptions]]></category>
		<category><![CDATA[ui]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://blog.redinnovation.com/?p=64</guid>
		<description><![CDATA[I bumped up into a problem where the object references where resolved as object copies when I passed them to class instances. That might sound easy to resolve, but unfortunately I was already deep in code and it was difficult to see this. Therefore, here&#8217;s a little explanation for those who are facing the same [...]]]></description>
			<content:encoded><![CDATA[<p>I bumped up into a problem where the object references where resolved as object copies when I passed them to class instances. That might sound easy to resolve, but unfortunately I was already deep in code and it was difficult to see this. Therefore, here&#8217;s a little explanation for those who are facing the same frustrating issue.</p>
<p>Say, you have variable &#8216;a&#8217; and you want to pass it to a MooTools class B instance during creation. In the easiest case you&#8217;d use new B({ myReference: a}) and trust on MooTools&#8217; Class.setOptions() to minify the need of code lines. This is what you should do&#8230; well at least that&#8217;s what I did and in this case it was a mistake.</p>
<p>It turns out that Class.setOptions() merges it&#8217;s arguments to this.options and then takes copy of them via $merge(). That means that any variable references you pass to setOptions() will get copied to this.options and.. well, that&#8217;s it. See lines 1170-1173 in uncompressed version of MooTools 1.2:</p>
<pre>var Options = new Class({
    setOptions: function(){
        this.options = $merge.run([this.options].extend(arguments));</pre>
<p>That effectively nullifies the benefits of Class.setOptions() if you want to pass in variable references..</p>
<p>Here&#8217;s a longer example to clarify (use Firebug):</p>
<pre>  // The most basic MooTools class that implements options
  // ref is a variable meant for pointing at given object
  // (won't do that, however)
  var B = new Class({
    Implements: Options,
    options: {
      ref: null
    },
    initialize: function(options) {
      this.setOptions(options);
    }
  });

  // Ok let's create an instance that we can pass to B
  // It's similar with all sorts of variables
  var A = new Class({
    initialize: function() {
      this.somevar = 'untouched';
    }
  });
  var a = new A();

  // Create an instance of B and give it somevar as reference
  var b = new B({ ref: a });

  // prints out "untouched" as should
  console.log(b.options.ref.somevar);

  // Let's change the variable (direct access, bad)
  a.somevar = "changed";

  // b's reference should still point to a, right?
  // In that case the following should print "changed",
  // but because our reference object was copied instead
  // of retaining reference to it, we just get "untouched"
  console.log(b.options.ref.somevar);</pre>
<p>I don&#8217;t know why MooTools wants to make a copy of arguments in setOptions() &#8211; propably for performance reasons.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mfabrik.com/2008/08/19/mootools-setoptions-nullifies-object-references/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

