Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>Given the example above, what is the difference between an object and a function?</p> </blockquote> <p>In Chrome, all these items are functions. In general however, a function is an object with the addition that it holds code and that you can call it. So, you can also just add properties to functions (like jQuery does: <code>$("selector")</code> or <code>$.ajax</code>).</p> <blockquote> <p>Do I write functions or objects in ES/JS?</p> </blockquote> <p>Well, obviously that depends on what you code. <code>function() {}</code> gives you a function; <code>{}</code> gives you an object. (Again, functions <em>are</em> objects in the end.)</p> <blockquote> <p>Why is Object a function and not an object?</p> </blockquote> <p><code>Object</code> is a function because you can call it, either as a constructor or not:</p> <pre><code>Object(); // returns an empty object new Object(); // same </code></pre> <p>Also, given that almost everything is an instance of <code>Object</code>, it follows that <code>Object</code> is a constructor and thus a function. (Note again that functions are also objects.)</p> <blockquote> <p>Is there any hierarchical structure to built in objects / functions?</p> </blockquote> <p>As for the ECMAScript built-in objects, there is in a sense. There are constructor functions (<code>String</code>) on the global object, functions for instances (<code>Array.prototype.forEach</code>), and "static" functions (<code>Object.defineProperty</code> which is meant to be used on objects, <code>Array.isArray</code> for arrays).</p> <blockquote> <p>Can built in objects / functions be redefined as something entirely different?</p> </blockquote> <p>Sure, you can do <code>Object = null</code>. But any code relying on <code>Object</code> will start throwing exceptions, so it's not recommended at all.</p> <blockquote> <p>Can built in objects / functions be undefined?</p> </blockquote> <p>No, an object is not undefined by definition. <code>undefined</code> is not an object and vice-versa. This holds for any object.</p> <blockquote> <p>Can built in objects / functions be assigned new features if they do not already support them natively?</p> </blockquote> <p>Yes, if e.g. <code>Array.prototype.forEach</code> does not exist, you could set it yourself. But it should be noted that such functions turn up in <code>for(var key in arr)</code> loops which again can cause code to behave differently. This can be solved using <code>Object.defineProperty</code> by using <code>{enumerable: false}</code>. But there is another caveat: the function is shared across the whole environment (e.g. the current page). If other code is also setting them you're experiencing collisions.</p> <blockquote> <p>If an object is defined in one browser and not another, how can I compensate for this?</p> </blockquote> <p>You can "shim" such functions. For e.g. ES5 functions such as <code>Array.prototype.forEach</code> there are shims available which make them available on older browsers as well. Underscore.js may be a good example.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    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