Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I find "prototype" values in javascript?
    text
    copied!<p>I use the following function to detect values that belonged to an object's <em>constructor</em> instead of the object itself.</p> <pre><code>function isAPrototypeValue(object, key ) { return !(object.constructor &amp;&amp; object.constructor.prototype[key]); } </code></pre> <p>this would work as follows:</p> <pre><code>Array.prototype.base_value = 'base' var array = new Array; array.custom_value = 'custom' alert( isAPrototypeValue( array, 'base_value' ) ) // true alert( isAPrototypeValue( array, 'custom_value' ) ) // false </code></pre> <p><strong><em>But</em></strong> when I started to use inheritance:</p> <pre><code>function Base() { return this }; Base.prototype.base_value = 'base'; function FirstSub() { return this }; FirstSub.prototype = new Base(); FirstSub.prototype.first_value = 'first'; function SubB () { return this }; SecondSub.prototype = new FirstSub(); SecondSub.prototype.second_value = 'second'; result = new SecondSub(); </code></pre> <p>and I called</p> <pre><code>alert( result.constructor ) </code></pre> <p>I would get <strong>Base</strong> instead of the expected <strong>SecondSub</strong>, which itself is not a big problem, But...</p> <p>if I extended <strong>result</strong> like this:</p> <pre><code>result.custom_value = 'custom' result.another_value = 'another' </code></pre> <p>I would have expected to be able to distinguish between values that belong to <strong>result</strong> or values that belong to <strong>SecondSub, FirstSub and Base</strong>;</p> <p>eg.</p> <pre><code>alert( isAPrototypeValue( result, 'custom_value' ) ) // false ( as expected ) alert( isAPrototypeValue( result, 'base_value' ) ) // true ( as expected ) alert( isAPrototypeValue( result, 'first_value' ) ) // true extend, but it is false alert( isAPrototypeValue( result, 'second_value' ) ) // true extend, but it is false </code></pre> <p>How can I change <strong>isAPrototypeValue</strong> to churn out the expected results?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload