I am currently learning advanced JavaScript, with an aim to build a standards compliant (HTML5, CSS3, ESv5) library. Along my way I have already asked a couple of related questions to try and figure out where to start, what to do, what not to do, what to avoid etc. I have already begun reading the ECMA-262 (ECMAScript version 5) documentation, and have been running a few tests before I get started on development work.
Previous questions:
Writing ECMAScript5 compliant code
What's the difference between JavaScript, JScript & ECMAScript?
In my research I found out that different browsers implement the standard differently, and in that respect, they implement different objects. For example, IE implements an object called ActiveXObject, but this is not the case in FireFox. So I wrote a little test facility which determines if something is defined within the browser.
Consider the following which tests a few known objects (including jQuery since this is not built in).

Again, I have reached a point where I am in need of help:
Questions:
-
Given the example above, what is the difference between an object and a function?
-
Do I write functions or objects in ES/JS?
-
Why is Object a function and not an object?
-
Is there any hierarchical structure to built in objects / functions?
-
Can built in objects / functions be redefined as something entirely different?
-
Can built in objects / functions be undefined?
-
Can built in objects / functions be assigned new features if they do not already support them natively?
-
If an object is defined in one browser and not another, how can I compensate for this?
P.S. I do not want answers relating to specific implementations (JavaScript/JScript), rather answers relating to the standard (ECMAScript v5). Thanks in advance!
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:
$("selector")or$.ajax).Well, obviously that depends on what you code.
function() {}gives you a function;{}gives you an object. (Again, functions are objects in the end.)Objectis a function because you can call it, either as a constructor or not:Also, given that almost everything is an instance of
Object, it follows thatObjectis a constructor and thus a function. (Note again that functions are also objects.)As for the ECMAScript built-in objects, there is in a sense. There are constructor functions (
String) on the global object, functions for instances (Array.prototype.forEach), and “static” functions (Object.definePropertywhich is meant to be used on objects,Array.isArrayfor arrays).Sure, you can do
Object = null. But any code relying onObjectwill start throwing exceptions, so it’s not recommended at all.No, an object is not undefined by definition.
undefinedis not an object and vice-versa. This holds for any object.Yes, if e.g.
Array.prototype.forEachdoes not exist, you could set it yourself. But it should be noted that such functions turn up infor(var key in arr)loops which again can cause code to behave differently. This can be solved usingObject.definePropertyby using{enumerable: false}. 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.You can “shim” such functions. For e.g. ES5 functions such as
Array.prototype.forEachthere are shims available which make them available on older browsers as well. Underscore.js may be a good example.