Object-Oriented JavaScript: An Introduction to Core Concepts
JavaScript is a powerful object-oriented language whose capability has frequently been underestimated when compared with other OO languages. This article, the first of several parts, should help you gain a better understanding of JavaScript's true value. It will show you the basics of creating custom objects and defining their methods and properties. You will also learn some advanced concepts such as Inheritance. Plenty of real-world examples will be included.
Object-Oriented JavaScript: An Introduction to Core Concepts - Functions and objects: a closer look at the “Function” object (Page 4 of 5 )
A less-known concept in JavaScript is that each function is itself an object. The reason for this rests on the automatic creation of a “Function” object each time a regular function is defined, without specifying the “new” keyword, as one would expect to do with conventional objects. To understand this concept, have a look at the example below, which creates a “Div” function, and then adds some properties to the “Function” object:
In the example above, I’ve defined a “Div()” function, which automatically has created a “Function” object. Provided that an object has some properties and methods, what I’ve done is simply assign a few properties to this “Div” object, in addition to the “display()” method. Finally, the method can be called up as you’d do normally with a regular object, as following:
Div.display();
Even when the “Function” object actually isn’t very popular, and certainly isn’t used very frequently in JavaScript applications, the above example demonstrates the basics of how to use it appropriately in your scripts.
After having explained some of the key points regarding the unusual “Function” object, let’s move on and continue learning more about user-defined objects in JavaScript. In this case we will take a look at the “constructor” property. Want to know more? Right, let’s explore this topic together.