The fifth and last way of creating objects in JavaScript is using the return in a function, you just return an object. So you just create a function and just add the return keyword. And then you create curly braces indicating an object, and you add your properties that you want to return. And it is going, it's going to return this object once he call this function. So let's get an example. And by the way, this for this function is also known as a factory function.
So let's get an example. So you just create a function for the factory, or method, or a factory or a factory function, you just create a function and call it person. And then you add the values that you want to pass name, as well as age, and then curly braces and inside, you just create an object a simple object. For example, if I create var person equals two curly braces, and then you want the name, you want to return the name, name equals the name that you pass, which is this one, and then the age, and then the age that you pass. But the thing is that this one, this variable is redundant. We don't, we don't have to here to create war person, it's redundant, you just can, instead of just typing your return, and then person, it's fine.
To do that you just create here, the object by using var person equals two curly braces and then the values and then you return that object. But this is not commonly used in JavaScript in JavaScript, what you would do instead, in order to make up your code shorter, you just delete this. It's redundant. As I told you, as well as this equal, and you just return, return this object, this object complete object. And since we pass these parameters, we don't even have to explicitly add the key and value pair, what we can do is just type name, and just type age. And JavaScript will immediately know that these values the name as well as the age are equal to the values that you pass.
But if the value does not exist as a parameter, you can just you can add it here expensively by using the key value pair. So here, I can just type. If I want another property, for example, a height, I would just say pi. And then the value which could be our default value, one, Aries centimeter so this is the fifth the fifth method. In order to create an object of this function, you just type, you just type, you just call the function without anything, just call the function person. And then you pass your parameters.
As a normal function, I'm going to pass key, as well as 25. And I'm going to store the result the return of this function into a variable called person one equals to this. And as you can see, what what this function what this function is going to return, it's going to return this object, which means that person one now equals to this object to this object. And of course, we can print it out using the console dot log, and then we pass person one, and semicolon if I save and run, I get this object with these three properties and we can also create as many variations as we want from this object. So I can here create another person called person, person two. And it's going to be equal to again, person.
But remember that this is a call to this function. So person, and they can pass whatever you want. So here I'm going to pass john and the age 23. And they can of course, again printed using console dot lock, and I pass person to and I Iran again. As you can see, I get another object in our inside person too. So this fifth method, which is called factory function is super powerful because you can create as many variations as you want from the same function.
So you just create the function one time and you create as many objects as you want.