Creating JavaScript Objects Using Constructors rating not rated
SoftXML
                  Programming Books
                Open Web Directory
           SoftXMLLib | SoftEcartJS
xml products   Home page    contact    site map
Contact Us
Latest Blog Articles:   Latest SEO Articles | RSS Feed
DOM manipulation best practices
JavaScript Module Pattern
How to start developing with the Nextjs
Angular application structure
Write Guest Articles
Articles Archive




Loading...

The Art & Science of JavaScript ($29 Value FREE For a Limited Time)
The Art & Science of JavaScript ($29 Value FREE For a Limited Time)









Creating JavaScript Objects Using Constructors


There are no classes in the JavaScript language, and this circumstance provides considerable flexibility, since you do not need to know anything about your object in advance - you do not need a class that will play the role of a prototype.

In JavaScript, there are constructor functions, syntax of calling these functions are borrowed from others object oriented languages like Java.

You can create objects using your own constructor functions or built-in constructors such as Object (), Date (), String (), and so on.

The following example demonstrates two equivalent ways to create two identical objects:


// first way - using a literal
var car = {color: “blue”};

// another way - using the built-in constructor
// attention: this is an anti-pattern

var car = new Object ();
car.color = “blue”;

In this example, one of the advantages of literals is immediately apparent - a shorter form of notation. Another reason literals are the preferred way to create objects is because this form of writing emphasizes once again that objects are just hashes that are editable, and not something special that needs to be baked for a specific "Recipe" (based on class).

Another advantage of using literals over the Object() constructor is that there is no need to resolve names in different scopes.

Since a local constructor with the same name can be created in the local scope, the interpreter will have to look at the entire chain of scopes from place of calling Object() until a global constructor Object() is found.

Disadvantages of the Object constructor

There is no reason to use the new Object() constructor when you can create objects in the form of literals, but you may encounter outdated program code written by other developers, so you should be aware of one “characteristic” of this constructor (and get acquainted with another reason against its use).

The problem is that the Object() constructor accepts a parameter and, depending on its value, can delegate the creation of the object to another built-in constructor, returning as a result the object that is not of the type that you expect.

The following are some examples of passing a number, a string, and a boolean to new Object(). As a result, in each case, objects created by various constructors are returned:


// Warning: all of these examples are anti-patterns
// empty object

var o = new Object();
console.log (o.constructor === Object); // true

// object-number
var o = new Object(7);
console.log (o.constructor === Number); // true
console.log (o.toFixed (2)); // “7.00”

// string object
var o = new Object(“I am the world”);
console.log (o.constructor === String); // true

// regular objects do not have a substring() method
// but this method is available for string objects
console.log (typeof o.substring); // “function”

// boolean
var o = new Object(true);
console.log (o.constructor === Boolean); // true

Such behavior of the Object() constructor can lead to unexpected results when the value passed to it is generated dynamically and its type is not known in advance. Once again, I urge you not to use the new Object() constructor. Use a simpler and more reliable form for writing objects as literals.

Tag cloud

() (oconstructor // ===

JavaScript

Object Object()

advance

builtin

calling

car

constructor

constructors

created

example

examples

form functions literals

local

object objects reason

result

string

true //

Rate This Article
(votes 1)

No Comments comments

Post Comment

We love comments on this blog - they are as important as anything we write ourself. They add to the knowledge and community that we have here. If you want to comment then you�re more than welcome � whether you feel you are a beginner or an expert � feel free to have you say.



* = required
Leave a Reply
Name *:
Email *
(will not be published):
Website:
Comment *:
Human Intelligence Identification *:
What is the background color of this web page?
  
Close
Please enter a valid email Please enter a valid name Please enter valid email Please enter valid name Enter valid year
™SoftXML.   Privacy Statement  |  Article Archive  |  Popular Web Development Books
^Top