Note that there are some explanatory texts on larger screens.

plurals
  1. POIt is said that all Javascript objects have a prototype property, but I only see foo.prototype if foo is a function?
    primarykey
    data
    text
    <p>It is often said that every Javascript object has a <code>prototype</code> property, but I find that <code>foo.prototype</code> has a value only if <code>foo</code> is a function.</p> <p>On Chrome and Firefox, <code>obj.__proto__</code> has a value -- is this the said <code>prototype</code> property? But on IE 9, it won't work (is there some way that can?), and I thought by <code>prototype</code> property, that means <code>obj.prototype</code> should work?</p> <p>I understand that <code>Object.getPrototypeOf(obj)</code> seems to show this <code>prototype</code> property, but why need a special method to get it? Why not just like <code>person.name</code>, which is to get the <code>name</code> property of the <code>person</code> object?</p> <hr> <p><strong>Update:</strong> by the way, <code>obj.constructor.prototype</code> seems to <strong>sometimes</strong> be that prototype, but sometimes not, as in the following code done with Prototypal inheritance with no constructor: (this method is in the Pro Javascript Design Patterns book by Harmes and Diaz by Apress 2008, p. 46)</p> <pre><code>var Person = { name: 'default value', getName: function() { return this.name; } } var reader = clone(Person); console.log(reader.getName()); reader.name = "Ang Lee"; console.log(reader.getName()); function clone(obj) { function F() {}; F.prototype = obj; return new F; } console.log("the prototype of reader is", Object.getPrototypeOf(reader)); console.log(Object.getPrototypeOf(reader) === reader.constructor.prototype); console.log(Object.getPrototypeOf(reader) == reader.constructor.prototype); console.log(Object.getPrototypeOf(reader) === reader.__proto__); console.log(Object.getPrototypeOf(reader) == reader.__proto__); </code></pre> <p>the result will show false, false, true, true for the last 4 lines.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

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