• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 23. Cookies and Creating Your Ow... > Creating Custom Objects: Properties

Creating Custom Objects: Properties

Suppose that we want to keep track of some friends, including their names, phone numbers, and our own personal ranking of them. Unfortunately, JavaScript doesn’t come with a friend object built in with these properties, but we can create one.

So how do you create a custom object? One way is to create a function with the same name as the object type you want to create, and to treat that function as the object’s constructor, which you use to create objects of that type. (We discussed constructors as far back as Chapter 2, “The JavaScript Language: Data, Operators, and Branching.”) For example, here’s how we might create a constructor for friend objects, where we’re passing the friend’s name, phone, and our ranking of that friend:


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint