Object Building Function

Object Properties

Object Building Function

🙋 Need help? Ask an expert now!

Object building functions are functions that are written to create objects. These functions return an object based on the parameters when the function is called. Object building functions are extremely useful as they create reusable code that can be run multiple times for a similar result. Take a look at the example below.

The function in the code sample above creates a new object based on the parameters first_name and last_name. The object consists of three properties, one of which is a function fullName. The function returns an object when it is called, which is why the variables george and chad are now an object. The use of the keyword new is crucial as it creates a new instance of the function using the given inputs. Printing an object results in a format similar to the object itself, with the property name as well as the property value.

Edit Me on GitHub!

Application Question

Consider the following code segment:

var bill;
function person(first_name, last_name) {
  return {
    first_name: first_name,
    last_name: last_name,
    fullName: function () {
      return this.first_name + ' ' + this.last_name;
    }
  }
}
bill = new person("Bill", "Brown");
bill.fullName();
console.log(bill.firstName);

What will the following code segment print to the console?