Click here to Skip to main content
13,596,324 members
Click here to Skip to main content
Add your own
alternative version


13 bookmarked
Posted 6 Nov 2012
Licenced CPOL

JavaScript Constructor With Configuration Object

, 4 Apr 2013
Rate this:
Please Sign up or sign in to vote.
Object constructor with another object instead of named variables.


This tip shows you how to initialize a JavaScript object, without having named constructors, but with another object.

The Classic Way

Let's say we have a person object with a name and an age. We would need to create a constructor that takes the name and age as arguments.

var person=function(name, age)  {;
var john=new person("john", 20);

From experience, I've found that I often like to initialize an object by sending in another object that holds the configuration for the new instance.

This can be done by using arguments[0];.

The Code

var person=function()   {
    for(var prop in arguments[0])   {

var john=new person({
    name: "john",
    age: 20

Now I can extend the object on the fly.

Prevent Object Extension

It's pretty cool that I could extend the object by sending in a function as one of the properties of the object that's being sent into the constructor. But sometimes, I don't want this to be possible. In that case, I declare all legal properties before I copy the configuration and then check if this has the property being sent in.

var person=function()   {;
    for(var prop in arguments[0])   {
        if(this.hasOwnProperty(prop))   {

Now it will no longer be possible to add properties that weren't declared.

An Example of Use

I find this trick most useful when serializing server data into a working object.

Let's assume we'd have this JSON result.

    "firstname": "John",
    "lastname": "Smith",
    "age": 36

And we'd have the following prototype;

var person=function()   {
    for(var prop in arguments[0])   {
    return this.firstname + " " + this.lastname;

We can now easily create working instances of Person out of Ajax json requests.

var user=null;
    url: "data.json",
    success: function(data) {
        //assuming the server has set content-type:application/json
        //jquery will serialize the json for you
        user=new Person(data);


  • April-5-2013: Added Ajax serialize example


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Sebastiaan Meijerink
Software Developer (Senior)
Netherlands Netherlands
I'm a developer with 15+ years of experience. Starting of on a MVS mainframe, moving to building big multi-tier ERP systems with unix backends, to building web-based BI-Portals. I've seen a lot of different languages, environments and paradigmes.

At this point my main interest is webdevelopment. Mainly javascript and ASP.NET. But I also like getting my hands dirty on some PHP.

My main focus has been shifting towards full javascript the past years. Almost anything can be accomplished now. From building full offline webapps, to IoT (Tessel), to server (node).

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
jahmani29-Mar-13 10:05
memberjahmani29-Mar-13 10:05 
GeneralMy vote of 5 Pin
nipunasilva29-Mar-13 5:53
membernipunasilva29-Mar-13 5:53 
GeneralMy vote of 5 Pin
Niral Soni9-Nov-12 0:25
memberNiral Soni9-Nov-12 0:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03-2016 | 2.8.180621.3 | Last Updated 5 Apr 2013
Article Copyright 2012 by Sebastiaan Meijerink
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid