Need help?

I'm available for remote short term contracting or consultancy work. Please check out my LinkedIn profile for more details on my experience.

Please feel free to use the form below to contact me.




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 (0)

Comments


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

captcha