Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


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

1. JavaScript Objects > User-Defined/Non-Native Object Constructor Functions

User-Defined/Non-Native Object Constructor Functions

As you saw with the Person() constructor, we can make our own constructor functions, from which we can produce not just one but multiple custom objects.

Below, I present the familiar Person() constructor function:

Live Code

<!DOCTYPE html><html lang="en"><body><script>

var Person = function(living, age, gender) {
    this.living = living;
    this.age = age;
    this.gender = gender;
    this.getGender = function() {return this.gender;};
};

var cody = new Person(true, 33, 'male');
console.log(cody); // logs Object {living=true, age=33, gender="male", ...}

var lisa = new Person(true, 34, 'female');
console.log(lisa); // logs Object {living=true, age=34, gender="female", ...}

</script></body></html>

  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free 10-Day Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint