1. Computing

Object Oriented JavaScript

6. Creating New Objects Without Copying Existing Ones

From , former About.com Guide

You don't have to copy an existing object in order to be able to create a new object in JavaScript.Well actually you do because all new objects in JavaScript are created based on the Object object. You do not however need to actually define them explicitly as new Object.

There are actually several different ways to define new objects in JavaScript and which one you choose to use will depend on exactly what combination of properties and methods you need the object to have.

One way of defining a new object is to define it exactly the same way you would define a function.


function myObject(arg1, arg2) {...}

There are two things that determine that this code will create an object rather tham a method of the window object (which is all a regular function is).

The first of these is what is inside of the function (where I have the ...) which in the case of an object constructor will be the code to create the object rather than code that a method is to execute. The second of these is the way that you call an object constructor which is different from the way you call a method.

In fact you have already seen what this second difference is in the way that you have written your code to use the built in objects within your code. To make it clearer let's look at two calls to a method and an object both of which are built into JavaScript (of course the same will apply to any objects and methods you create yourself).


var now = Date();
var today = new Date();

The first of these runs the built in Date method and returns the value which is stored in a window property called now. The second of these creates a new object called today as a copy of the Date object. In the second of these if we are not passing any parameters into the Date object we can omit the parentheses off the end and the statement will still do the exact same thing. If we leave the parentheses off the end of the first statement then instead of running the Date method and assigning the result to now we would instead define now as an alternative name for the Date method and running now() would be the same as running Date().

The difference between these two is of course the keyword "new" which appears in front of the name of the object that we are using as the base for creating our new object. So if we want to create a new object based on our myObject definition from earlier we'd use:


var myNewObj = new myObject(parm1, parm2);

This creates a new object based on the object constructor that we have defined. Using this way of creating a new object means that we have first created the object constructor with a name (in this case myObject) and then run it in order to create the object we want to actually use in our script. We can also write the code in such a way as to not need to give the object constructor a name in order to use it to create an object (which is what you might do if you expect to only need one object of that type.


var myNewObj = new function(arg1,arg2) {...){parm1,parm2);

This version does exactly the same thing as before but does not give a name to the object constructor.

All the Object Oriented JavaScript Tutorials

  1. What is Object Oriented JavaScript?
  2. The Benefits of OO JavaScript
  3. JavaScript's Built in Objects
  4. Extending Built In Objects
  5. Creating Objects from Existing Objects
  6. Creating New Objects Without Copying Existing Ones
  7. Dot Notation and "this"
  8. prototype
  9. Inheritance and "constructor"
  10. Associative Array Notation
  11. Create Method if Doesn't Exist
  12. "self" and Multiple Objects
  13. Defining Objects with JSON
  14. Namespaces
  15. Lazy Definition
  16. Extending Methods
  17. Copying Objects
  18. Private Properties and Privileged Methods
  19. Public Access to Private Methods
  20. Chaining Methods
  21. Singletons

©2013 About.com. All rights reserved.