OOP in JavaScript
Let's start by looking at how you could define a person with a normal function. Add this function within the script element: function createNewPerson ( name ) { var obj = { } ; obj . name = name ; obj . greeting = function ( ) { alert ( 'Hi! I\'m ' + obj . name + '.' ) ; } ; return obj ; } You can now create a new person by calling this function — try the following lines in your browser's JavaScript console: var salva = createNewPerson ( 'Salva' ) ; salva . name ; salva . greeting ( ) ; This works well enough, but it is a bit long-winded; if we know we want to create an object, why do we need to explicitly create a new empty object and return it? Fortunately, JavaScript provides us with a handy shortcut, in the form of constructor functions — let's make one now! Replace your previous function with the following: function Person ( name ) { this . name = name ; this . greeting = function ( ) { ...