oop.js.html revision 4fcbec6145d16637205990699912fb90f6a3807c
<html>
<head>
</head>
<body id="yahoo-com">
<div id="doc3" class="yui-t2">
<div id="hd">
<h3>oop <span class="subtitle">3.00PR1</span></h3>
<p>
> oop.js (source view)
</p>
</div>
<div id="bd">
<div id="yui-main">
<div class="yui-b">
<form name="yui-classopts-form">
<span id="classopts"><input type="checkbox" name="showprivate" id="showprivate" /> <label for="showprivate">Show Private</label></span>
<span id="classopts"><input type="checkbox" name="showprotected" id="showprotected" /> <label for="showprotected">Show Protected</label></span>
<span id="classopts"><input type="checkbox" name="showdeprecated" id="showdeprecated" /> <label for="showdeprecated">Show Deprecated</label></span>
</form>
<div id="srcout">
<style>
#doc3 #classopts { display:none; }
</style>
<div class="highlight" ><pre><span class="c">/**</span>
<span class="c"> * OOP utils. If included, the OOP methods are added to the YUI instance</span>
<span class="c"> * @module oop</span>
<span class="c"> */</span>
<span class="nx">YUI</span><span class="o">.</span><span class="nx">add</span><span class="o">(</span><span class="s2">"oop"</span><span class="o">,</span> <span class="k">function</span><span class="o">(</span><span class="nx">Y</span><span class="o">)</span> <span class="o">{</span>
<span class="k">var</span> <span class="nx">L</span> <span class="o">=</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">Lang</span><span class="o">,</span>
<span class="nx">A</span> <span class="o">=</span> <span class="nx">Y</span><span class="o">.</span><span class="nb">Array</span><span class="o">,</span>
<span class="nx">OP</span> <span class="o">=</span> <span class="nb">Object</span><span class="o">.</span><span class="nx">prototype</span><span class="o">,</span>
<span class="nx">O</span><span class="o">;</span>
<span class="c">/**</span>
<span class="c"> * The following methods are added to the YUI instance</span>
<span class="c"> * @class YUI~oop</span>
<span class="c"> */</span>
<span class="c">/**</span>
<span class="c"> * Applies prototype properties from the supplier to the receiver.</span>
<span class="c"> * The receiver can be a constructor or an instance.</span>
<span class="c"> * @method augment</span>
<span class="c"> * @param {Function} r the object to receive the augmentation</span>
<span class="c"> * @param {Function} s the object that supplies the properties to augment</span>
<span class="c"> * @param ov {boolean} if true, properties already on the receiver</span>
<span class="c"> * will be overwritten if found on the supplier.</span>
<span class="c"> * @param wl {string[]} a whitelist. If supplied, only properties in </span>
<span class="c"> * this list will be applied to the receiver.</span>
<span class="c"> * @param args {Array | Any} arg or arguments to apply to the supplier</span>
<span class="c"> * constructor when initializing.</span>
<span class="c"> * @return {object} the augmented object</span>
<span class="c"> *</span>
<span class="c"> * @todo constructor optional?</span>
<span class="c"> * @todo understanding what an instance is augmented with</span>
<span class="c"> * @TODO evaluate if we can do this in a way that doesn't interfere</span>
<span class="c"> * with normal inheritance</span>
<span class="c"> */</span>
<span class="nx">Y</span><span class="o">.</span><span class="nx">augment</span> <span class="o">=</span> <span class="k">function</span><span class="o">(</span><span class="nx">r</span><span class="o">,</span> <span class="nx">s</span><span class="o">,</span> <span class="nx">ov</span><span class="o">,</span> <span class="nx">wl</span><span class="o">,</span> <span class="nx">args</span><span class="o">)</span> <span class="o">{</span>
<span class="k">var</span> <span class="nx">sProto</span> <span class="o">=</span> <span class="nx">s</span><span class="o">.</span><span class="nx">prototype</span><span class="o">,</span> <span class="nx">newProto</span> <span class="o">=</span> <span class="kc">null</span><span class="o">,</span> <span class="nx">construct</span> <span class="o">=</span> <span class="nx">s</span><span class="o">,</span>
<span class="nx">a</span> <span class="o">=</span> <span class="o">(</span><span class="nx">args</span><span class="o">)</span> <span class="o">?</span> <span class="nx">Y</span><span class="o">.</span><span class="nb">Array</span><span class="o">(</span><span class="nx">args</span><span class="o">)</span> <span class="o">:</span> <span class="o">[],</span> <span class="nx">rProto</span> <span class="o">=</span> <span class="nx">r</span><span class="o">.</span><span class="nx">prototype</span><span class="o">,</span>
<span class="nx">target</span> <span class="o">=</span> <span class="nx">rProto</span> <span class="o">||</span> <span class="nx">r</span><span class="o">,</span> <span class="nx">applyConstructor</span> <span class="o">=</span> <span class="kc">false</span><span class="o">;</span>
<span class="c">// working on a class, so apply constructor infrastructure</span>
<span class="c"></span> <span class="k">if</span> <span class="o">(</span><span class="nx">rProto</span> <span class="o">&&</span> <span class="nx">construct</span><span class="o">)</span> <span class="o">{</span>
<span class="c"></span>
<span class="c"></span>
<span class="k">var</span> <span class="nx">sequestered</span> <span class="o">=</span> <span class="o">{},</span> <span class="nx">replacements</span> <span class="o">=</span> <span class="o">{};</span>
<span class="nx">newProto</span> <span class="o">=</span> <span class="o">{};</span>
<span class="c">// sequester all of the functions in the supplier and replace with</span>
<span class="c"></span> <span class="c">// one that will restore all of them.</span>
<span class="c"></span> <span class="nx">Y</span><span class="o">.</span><span class="nx">each</span><span class="o">(</span><span class="nx">sProto</span><span class="o">,</span> <span class="k">function</span><span class="o">(</span><span class="nx">v</span><span class="o">,</span> <span class="nx">k</span><span class="o">)</span> <span class="o">{</span>
<span class="c">// var initialized = false;</span>
<span class="c"></span>
<span class="nx">replacements</span><span class="o">[</span><span class="nx">k</span><span class="o">]</span> <span class="o">=</span> <span class="k">function</span><span class="o">()</span> <span class="o">{</span>
<span class="k">var</span> <span class="nx">me</span> <span class="o">=</span> <span class="k">this</span><span class="o">;</span>
<span class="c">// Y.log('sequestered function "' + k + '" executed. Initializing Event.Target');</span>
<span class="c"></span>
<span class="c">// overwrite the prototype with all of the sequestered functions,</span>
<span class="c"></span> <span class="c">// but only if it hasn't been overridden</span>
<span class="c"></span> <span class="k">for</span> <span class="o">(</span><span class="k">var</span> <span class="nx">i</span> <span class="k">in</span> <span class="nx">sequestered</span><span class="o">)</span> <span class="o">{</span>
<span class="k">if</span> <span class="o">(</span><span class="nx">Y</span><span class="o">.</span><span class="nb">Object</span><span class="o">.</span><span class="nx">owns</span><span class="o">(</span><span class="nx">sequestered</span><span class="o">,</span> <span class="nx">i</span><span class="o">)</span> <span class="o">&&</span> <span class="o">(</span><span class="nx">me</span><span class="o">[</span><span class="nx">i</span><span class="o">]</span> <span class="o">===</span> <span class="nx">replacements</span><span class="o">[</span><span class="nx">i</span><span class="o">]))</span> <span class="o">{</span>
<span class="c"></span> <span class="nx">me</span><span class="o">[</span><span class="nx">i</span><span class="o">]</span> <span class="o">=</span> <span class="nx">sequestered</span><span class="o">[</span><span class="nx">i</span><span class="o">];</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="c">// apply the constructor</span>
<span class="c"></span> <span class="nx">construct</span><span class="o">.</span><span class="nx">apply</span><span class="o">(</span><span class="nx">me</span><span class="o">,</span> <span class="nx">a</span><span class="o">);</span>
<span class="c">// apply the original sequestered function</span>
<span class="c"></span> <span class="k">return</span> <span class="nx">sequestered</span><span class="o">[</span><span class="nx">k</span><span class="o">].</span><span class="nx">apply</span><span class="o">(</span><span class="nx">me</span><span class="o">,</span> <span class="nx">arguments</span><span class="o">);</span>
<span class="o">};</span>
<span class="k">if</span> <span class="o">((!</span><span class="nx">wl</span> <span class="o">||</span> <span class="o">(</span><span class="nx">k</span> <span class="k">in</span> <span class="nx">wl</span><span class="o">))</span> <span class="o">&&</span> <span class="o">(</span><span class="nx">ov</span> <span class="o">||</span> <span class="o">!(</span><span class="nx">k</span> <span class="k">in</span> <span class="k">this</span><span class="o">)))</span> <span class="o">{</span>
<span class="c"></span>
<span class="k">if</span> <span class="o">(</span><span class="nx">L</span><span class="o">.</span><span class="nx">isFunction</span><span class="o">(</span><span class="nx">v</span><span class="o">))</span> <span class="o">{</span>
<span class="c">// sequester the function</span>
<span class="c"></span> <span class="nx">sequestered</span><span class="o">[</span><span class="nx">k</span><span class="o">]</span> <span class="o">=</span> <span class="nx">v</span><span class="o">;</span>
<span class="c">// replace the sequestered function with a function that will</span>
<span class="c"></span> <span class="c">// restore all sequestered functions and exectue the constructor.</span>
<span class="c"></span> <span class="k">this</span><span class="o">[</span><span class="nx">k</span><span class="o">]</span> <span class="o">=</span> <span class="nx">replacements</span><span class="o">[</span><span class="nx">k</span><span class="o">];</span>
<span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
<span class="c"></span>
<span class="k">this</span><span class="o">[</span><span class="nx">k</span><span class="o">]</span> <span class="o">=</span> <span class="nx">v</span><span class="o">;</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="o">},</span> <span class="nx">newProto</span><span class="o">,</span> <span class="kc">true</span><span class="o">);</span>
<span class="c">// augmenting an instance, so apply the constructor immediately</span>
<span class="c"></span> <span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
<span class="nx">applyConstructor</span> <span class="o">=</span> <span class="kc">true</span><span class="o">;</span>
<span class="o">}</span>
<span class="nx">Y</span><span class="o">.</span><span class="nx">mix</span><span class="o">(</span><span class="nx">target</span><span class="o">,</span> <span class="nx">newProto</span> <span class="o">||</span> <span class="nx">sProto</span><span class="o">,</span> <span class="nx">ov</span><span class="o">,</span> <span class="nx">wl</span><span class="o">);</span>
<span class="k">if</span> <span class="o">(</span><span class="nx">applyConstructor</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">s</span><span class="o">.</span><span class="nx">apply</span><span class="o">(</span><span class="nx">target</span><span class="o">,</span> <span class="nx">a</span><span class="o">);</span>
<span class="o">}</span>
<span class="k">return</span> <span class="nx">r</span><span class="o">;</span>
<span class="o">};</span>
<span class="c">/**</span>
<span class="c"> * Applies object properties from the supplier to the receiver. If</span>
<span class="c"> * the target has the property, and the property is an object, the target</span>
<span class="c"> * object will be augmented with the supplier's value. If the property</span>
<span class="c"> * is an array, the suppliers value will be appended to the target.</span>
<span class="c"> * @method aggregate</span>
<span class="c"> * @param {Function} r the object to receive the augmentation</span>
<span class="c"> * @param {Function} s the object that supplies the properties to augment</span>
<span class="c"> * @param ov {boolean} if true, properties already on the receiver</span>
<span class="c"> * will be overwritten if found on the supplier.</span>
<span class="c"> * @param wl {string[]} a whitelist. If supplied, only properties in </span>
<span class="c"> * this list will be applied to the receiver.</span>
<span class="c"> * @return {object} the extended object</span>
<span class="c"> */</span>
<span class="nx">Y</span><span class="o">.</span><span class="nx">aggregate</span> <span class="o">=</span> <span class="k">function</span><span class="o">(</span><span class="nx">r</span><span class="o">,</span> <span class="nx">s</span><span class="o">,</span> <span class="nx">ov</span><span class="o">,</span> <span class="nx">wl</span><span class="o">)</span> <span class="o">{</span>
<span class="k">return</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">mix</span><span class="o">(</span><span class="nx">r</span><span class="o">,</span> <span class="nx">s</span><span class="o">,</span> <span class="nx">ov</span><span class="o">,</span> <span class="nx">wl</span><span class="o">,</span> <span class="m">0</span><span class="o">,</span> <span class="kc">true</span><span class="o">);</span>
<span class="o">};</span>
<span class="c">/**</span>
<span class="c"> * Utility to set up the prototype, constructor and superclass properties to</span>
<span class="c"> * support an inheritance strategy that can chain constructors and methods.</span>
<span class="c"> * Static members will not be inherited.</span>
<span class="c"> *</span>
<span class="c"> * @method extend</span>
<span class="c"> * @static</span>
<span class="c"> * @param {Function} r the object to modify</span>
<span class="c"> * @param {Function} s the object to inherit</span>
<span class="c"> * @return {YUI} the YUI instance</span>
<span class="c"> */</span>
<span class="nx">Y</span><span class="o">.</span><span class="nx">extend</span> <span class="o">=</span> <span class="k">function</span><span class="o">(</span><span class="nx">r</span><span class="o">,</span> <span class="nx">s</span><span class="o">,</span> <span class="nx">px</span><span class="o">,</span> <span class="nx">sx</span><span class="o">)</span> <span class="o">{</span>
<span class="k">if</span> <span class="o">(!</span><span class="nx">s</span><span class="o">||!</span><span class="nx">r</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">Y</span><span class="o">.</span><span class="nx">fail</span><span class="o">(</span><span class="s2">"extend failed, verify dependencies"</span><span class="o">);</span>
<span class="o">}</span>
<span class="k">var</span> <span class="nx">sp</span> <span class="o">=</span> <span class="nx">s</span><span class="o">.</span><span class="nx">prototype</span><span class="o">,</span> <span class="nx">rp</span><span class="o">=</span><span class="nx">Y</span><span class="o">.</span><span class="nb">Object</span><span class="o">(</span><span class="nx">sp</span><span class="o">),</span> <span class="nx">i</span><span class="o">;</span>
<span class="nx">r</span><span class="o">.</span><span class="nx">prototype</span><span class="o">=</span><span class="nx">rp</span><span class="o">;</span>
<span class="nx">rp</span><span class="o">.</span><span class="nx">constructor</span><span class="o">=</span><span class="nx">r</span><span class="o">;</span>
<span class="nx">r</span><span class="o">.</span><span class="nx">superclass</span><span class="o">=</span><span class="nx">sp</span><span class="o">;</span>
<span class="c">// If the superclass doesn't have a standard constructor,</span>
<span class="c"></span> <span class="c">// define one so that Super() works</span>
<span class="c"></span> <span class="k">if</span> <span class="o">(</span><span class="nx">s</span> <span class="o">!=</span> <span class="nb">Object</span> <span class="o">&&</span> <span class="nx">sp</span><span class="o">.</span><span class="nx">constructor</span> <span class="o">==</span> <span class="nx">OP</span><span class="o">.</span><span class="nx">constructor</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">sp</span><span class="o">.</span><span class="nx">constructor</span><span class="o">=</span><span class="nx">s</span><span class="o">;</span>
<span class="o">}</span>
<span class="c">// Add object properties too</span>
<span class="c"></span> <span class="c">// @TODO removed for now because it isn't that useful and</span>
<span class="c"></span> <span class="c">// has caused a few issues overwriting things that should</span>
<span class="c"></span> <span class="c">// not be. You can do this manually if needed. Revisit</span>
<span class="c"></span> <span class="c">// if this is something that really is needed for some</span>
<span class="c"></span> <span class="c">// reason.</span>
<span class="c"></span>
<span class="c">// Add superclass convienience functions </span>
<span class="c"></span> <span class="c">// @TODO revisit when we have something that works</span>
<span class="c"></span>
<span class="c">// Add prototype overrides</span>
<span class="c"></span> <span class="k">if</span> <span class="o">(</span><span class="nx">px</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">Y</span><span class="o">.</span><span class="nx">mix</span><span class="o">(</span><span class="nx">rp</span><span class="o">,</span> <span class="nx">px</span><span class="o">,</span> <span class="kc">true</span><span class="o">);</span>
<span class="o">}</span>
<span class="c">// Add object overrides</span>
<span class="c"></span> <span class="k">if</span> <span class="o">(</span><span class="nx">sx</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">Y</span><span class="o">.</span><span class="nx">mix</span><span class="o">(</span><span class="nx">r</span><span class="o">,</span> <span class="nx">sx</span><span class="o">,</span> <span class="kc">true</span><span class="o">);</span>
<span class="o">}</span>
<span class="k">return</span> <span class="nx">r</span><span class="o">;</span>
<span class="o">};</span>
<span class="c">/**</span>
<span class="c"> * Executes the supplied function for each item in</span>
<span class="c"> * a collection. Supports arrays, objects, and</span>
<span class="c"> * @method each</span>
<span class="c"> * @param o the object to iterate</span>
<span class="c"> * @param f the function to execute. This function</span>
<span class="c"> * receives the value, key, and object as parameters</span>
<span class="c"> * @param proto if true, prototype properties are</span>
<span class="c"> * iterated on objects</span>
<span class="c"> * @return {YUI} the YUI instance</span>
<span class="c"> */</span>
<span class="nx">Y</span><span class="o">.</span><span class="nx">each</span> <span class="o">=</span> <span class="k">function</span><span class="o">(</span><span class="nx">o</span><span class="o">,</span> <span class="nx">f</span><span class="o">,</span> <span class="nx">c</span><span class="o">,</span> <span class="nx">proto</span><span class="o">)</span> <span class="o">{</span>
<span class="k">if</span> <span class="o">(</span><span class="nx">o</span><span class="o">.</span><span class="nx">each</span> <span class="o">&&</span> <span class="nx">o</span><span class="o">.</span><span class="nx">item</span><span class="o">)</span> <span class="o">{</span>
<span class="k">return</span> <span class="nx">o</span><span class="o">.</span><span class="nx">each</span><span class="o">.</span><span class="nx">call</span><span class="o">(</span><span class="nx">o</span><span class="o">,</span> <span class="nx">f</span><span class="o">,</span> <span class="nx">c</span><span class="o">);</span>
<span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
<span class="nx">switch</span> <span class="o">(</span><span class="nx">A</span><span class="o">.</span><span class="nx">test</span><span class="o">(</span><span class="nx">o</span><span class="o">))</span> <span class="o">{</span>
<span class="nx">case</span> <span class="m">1</span><span class="o">:</span>
<span class="k">return</span> <span class="nx">A</span><span class="o">.</span><span class="nx">each</span><span class="o">(</span><span class="nx">o</span><span class="o">,</span> <span class="nx">f</span><span class="o">,</span> <span class="nx">c</span><span class="o">);</span>
<span class="nx">case</span> <span class="m">2</span><span class="o">:</span>
<span class="k">return</span> <span class="nx">A</span><span class="o">.</span><span class="nx">each</span><span class="o">(</span><span class="nx">Y</span><span class="o">.</span><span class="nb">Array</span><span class="o">(</span><span class="nx">o</span><span class="o">,</span> <span class="m">0</span><span class="o">,</span> <span class="kc">true</span><span class="o">),</span> <span class="nx">f</span><span class="o">,</span> <span class="nx">c</span><span class="o">);</span>
<span class="nx">default</span><span class="o">:</span>
<span class="k">return</span> <span class="nx">Y</span><span class="o">.</span><span class="nb">Object</span><span class="o">.</span><span class="nx">each</span><span class="o">(</span><span class="nx">o</span><span class="o">,</span> <span class="nx">f</span><span class="o">,</span> <span class="nx">c</span><span class="o">,</span> <span class="nx">proto</span><span class="o">);</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="c"></span> <span class="o">};</span>
<span class="c">/**</span>
<span class="c"> * Array-like objects are treated as arrays.</span>
<span class="c"> * primitives are returned untouched. Optionally a</span>
<span class="c"> * function can be provided to handle other data types,</span>
<span class="c"> * filter keys, validate values, etc.</span>
<span class="c"> *</span>
<span class="c"> * @method clone</span>
<span class="c"> * @param o what to clone</span>
<span class="c"> * @param safe {boolean} if true, objects will not have prototype</span>
<span class="c"> * items from the source. If false, it does. In this case, the</span>
<span class="c"> * original is initally protected, but the clone is not completely immune</span>
<span class="c"> * from changes to the source object prototype. Also, cloned prototype</span>
<span class="c"> * items that are deleted from the clone will result in the value</span>
<span class="c"> * of the source prototype to be exposed. If operating on a non-safe</span>
<span class="c"> * clone, items should be nulled out rather than deleted.</span>
<span class="c"> * @TODO review</span>
<span class="c"> * @param f optional function to apply to each item in a collection</span>
<span class="c"> * it will be executed prior to applying the value to</span>
<span class="c"> * the new object. Return false to prevent the copy.</span>
<span class="c"> * @param c optional execution context for f</span>
<span class="c"> * @param owner Owner object passed when clone is iterating an</span>
<span class="c"> * object. Used to set up context for cloned functions.</span>
<span class="c"> * @return {Array|Object} the cloned object</span>
<span class="c"> */</span>
<span class="nx">Y</span><span class="o">.</span><span class="nx">clone</span> <span class="o">=</span> <span class="k">function</span><span class="o">(</span><span class="nx">o</span><span class="o">,</span> <span class="nx">safe</span><span class="o">,</span> <span class="nx">f</span><span class="o">,</span> <span class="nx">c</span><span class="o">,</span> <span class="nx">owner</span><span class="o">)</span> <span class="o">{</span>
<span class="k">if</span> <span class="o">(!</span><span class="nx">L</span><span class="o">.</span><span class="nx">isObject</span><span class="o">(</span><span class="nx">o</span><span class="o">))</span> <span class="o">{</span>
<span class="k">return</span> <span class="nx">o</span><span class="o">;</span>
<span class="o">}</span>
<span class="k">if</span> <span class="o">(</span><span class="nx">L</span><span class="o">.</span><span class="nx">isDate</span><span class="o">(</span><span class="nx">o</span><span class="o">))</span> <span class="o">{</span>
<span class="k">return</span> <span class="k">new</span> <span class="nb">Date</span><span class="o">(</span><span class="nx">o</span><span class="o">);</span>
<span class="o">}</span>
<span class="k">var</span> <span class="nx">func</span> <span class="o">=</span> <span class="nx">L</span><span class="o">.</span><span class="nx">isFunction</span><span class="o">(</span><span class="nx">o</span><span class="o">),</span> <span class="nx">o2</span><span class="o">;</span>
<span class="k">if</span> <span class="o">(</span><span class="nx">func</span><span class="o">)</span> <span class="o">{</span>
<span class="k">if</span> <span class="o">(</span><span class="nx">o</span> <span class="k">instanceof</span> <span class="nb">RegExp</span><span class="o">)</span> <span class="o">{</span>
<span class="k">return</span> <span class="k">new</span> <span class="nb">RegExp</span><span class="o">(</span><span class="nx">o</span><span class="o">.</span><span class="nx">source</span><span class="o">);</span>
<span class="o">}</span>
<span class="nx">o2</span> <span class="o">=</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">bind</span><span class="o">(</span><span class="nx">o</span><span class="o">,</span> <span class="nx">owner</span><span class="o">);</span>
<span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
<span class="nx">o2</span> <span class="o">=</span> <span class="o">(</span><span class="nx">safe</span><span class="o">)</span> <span class="o">?</span> <span class="o">{}</span> <span class="o">:</span> <span class="nx">Y</span><span class="o">.</span><span class="nb">Object</span><span class="o">(</span><span class="nx">o</span><span class="o">);</span>
<span class="o">}</span>
<span class="nx">Y</span><span class="o">.</span><span class="nx">each</span><span class="o">(</span><span class="nx">o</span><span class="o">,</span> <span class="k">function</span><span class="o">(</span><span class="nx">v</span><span class="o">,</span> <span class="nx">k</span><span class="o">)</span> <span class="o">{</span>
<span class="k">if</span> <span class="o">(!</span><span class="nx">f</span> <span class="o">||</span> <span class="o">(</span><span class="nx">f</span><span class="o">.</span><span class="nx">call</span><span class="o">(</span><span class="nx">c</span> <span class="o">||</span> <span class="k">this</span><span class="o">,</span> <span class="nx">v</span><span class="o">,</span> <span class="nx">k</span><span class="o">,</span> <span class="k">this</span><span class="o">,</span> <span class="nx">o</span><span class="o">)</span> <span class="o">!==</span> <span class="kc">false</span><span class="o">))</span> <span class="o">{</span>
<span class="k">this</span><span class="o">[</span><span class="nx">k</span><span class="o">]</span> <span class="o">=</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">clone</span><span class="o">(</span><span class="nx">v</span><span class="o">,</span> <span class="nx">safe</span><span class="o">,</span> <span class="nx">f</span><span class="o">,</span> <span class="nx">c</span><span class="o">,</span> <span class="k">this</span><span class="o">);</span>
<span class="o">}</span>
<span class="o">},</span> <span class="nx">o2</span><span class="o">);</span>
<span class="k">return</span> <span class="nx">o2</span><span class="o">;</span>
<span class="o">};</span>
<span class="c">/**</span>
<span class="c"> * Returns a function that will execute the supplied function in the</span>
<span class="c"> * supplied object's context, optionally adding any additional</span>
<span class="c"> * supplied parameters to the end of the arguments the function</span>
<span class="c"> * is executed with.</span>
<span class="c"> * @TODO review param order for PR2</span>
<span class="c"> * @method bind</span>
<span class="c"> * @param f {Function} the function to bind</span>
<span class="c"> * @param c the execution context</span>
<span class="c"> * @param args* 0..n arguments to append to the arguments collection for the function</span>
<span class="c"> * @return {function} the wrapped function</span>
<span class="c"> */</span>
<span class="nx">Y</span><span class="o">.</span><span class="nx">bind</span> <span class="o">=</span> <span class="k">function</span><span class="o">(</span><span class="nx">f</span><span class="o">,</span> <span class="nx">c</span><span class="o">)</span> <span class="o">{</span>
<span class="c">// if (!f) {</span>
<span class="c"></span> <span class="c">// }</span>
<span class="c"></span> <span class="k">var</span> <span class="nx">a</span> <span class="o">=</span> <span class="nx">Y</span><span class="o">.</span><span class="nb">Array</span><span class="o">(</span><span class="nx">arguments</span><span class="o">,</span> <span class="m">2</span><span class="o">,</span> <span class="kc">true</span><span class="o">);</span>
<span class="k">return</span> <span class="k">function</span> <span class="o">()</span> <span class="o">{</span>
<span class="c">// @todo bind args first, or function args first?</span>
<span class="c"></span> <span class="c">// return f.apply(c || f, a.concat(Y.Array(arguments, 0, true)));</span>
<span class="c"></span> <span class="k">return</span> <span class="nx">f</span><span class="o">.</span><span class="nx">apply</span><span class="o">(</span><span class="nx">c</span> <span class="o">||</span> <span class="nx">f</span><span class="o">,</span> <span class="nx">Y</span><span class="o">.</span><span class="nb">Array</span><span class="o">(</span><span class="nx">arguments</span><span class="o">,</span> <span class="m">0</span><span class="o">,</span> <span class="kc">true</span><span class="o">).</span><span class="nx">concat</span><span class="o">(</span><span class="nx">a</span><span class="o">));</span>
<span class="o">};</span>
<span class="o">};</span>
<span class="o">},</span> <span class="s2">"@VERSION@"</span><span class="o">);</span>
</pre></div>
</div>
</div>
</div>
<div class="yui-b">
<div class="nav">
<div class="module">
<h4>Modules</h4>
<ul class="content">
</ul>
</div>
<div class="module">
<h4>Classes</h4>
<ul class="content">
</ul>
</div>
<div class="module">
<h4>Files</h4>
<ul class="content">
</ul>
</div>
</div>
</div>
</div>
<div id="ft">
<hr />
Copyright © 2008 Yahoo! Inc. All rights reserved.
</div>
</div>
</body>
</html>