How to extend an object in JavaScript

In this quick tutorial I'm going to go through a simple example of how you can extend an object in JavaScript. The first thing you'll need to do is get jQuery, which you can get here: http://docs.jquery.com/Downloading_jQuery.

Once you have that included in your project, you can go ahead and extend an object by doing the following:


var firstObject = {
    title: "Mr",
    name: "Bob Smith"
}

// here we're going to extend that first object
$.extend(firstObject, {
    address: "10 Brown Ave."
});

In this previous object extension example, we've extended an object that just had title and name, and added another property called 'address'.

Hope this helps someone.

jon | December 06, 2011 | Comments (2)

Comments

thanks alot very much
Comment by مطبخنا - May 16, 2012 @ 5:42 am
The article is composed in quite a superb method also it entails loads of useful information and details for me. I am happy to can be found throughout your distinguished method of crafting the post. Now you ensure it is uncomplicated for me to recognize and put into actions the concept. Thank you using the post.
Comment by movado amorosa watch - April 18, 2012 @ 8:48 pm

Name (required)
Email (will not be published) (required)
Website

captcha