Creating Custom Defined Objects

As you have seen so far, using the built-in JavaScript objects has several advantages. As you begin to write code that uses more and more data, you will find yourself wanting to build your own custom objects, with specific properties and methods.

You can define JavaScript objects in a couple of ways. The simplest is the on-the-fly method: Simply create a generic object and then add properties to it as needed. For example, to create a user object and assign a first and last name as well as define a function to return them, you could use the following code:

var user = new Object();user.first="Brad";user.last="Dayley";user.getName = function( ) { return this.first + " " + this.last; } ...

Get Learning AngularJS now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.