Manipulate an object returned by a JavaScript function

Hi there,

Using JavaScript, how do I add a method to the object that is returned by this function, without changing the function directly?

var myFunction = function(){    
    return {
      firstName: 'John',
        lastName: 'Doe'
    }
}

With an object literal, you do it like this below, but how to do it to an object that is inside a function??

// Object
var myObject = {
    firstName: 'John',
    lastName: 'Doe'
};

// Method added to object
myObject.sayHello = function() {
    console.log('Hello');
}
myObject.sayHello();

Thanks!
userTesterAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chaauCommented:
I think nothing's different:
JSBin
var myObject = myFunction();

 // Method added to object
 myObject.sayHello = function() {
     console.log('Hello');
 }
 myObject.sayHello();

Open in new window

Julian HansenCommented:
The previous post adds a method to the instantiated object only. If you want to add a method to the global scope of the object then you have to add the function to the object's prototype.
Consider the following sample
<script type="text/javascript">
// CREATE AN OBJECT
var myObj = function (name,age)
{
    this.name = name;
    this.age = age;

    // CREATE OBJECT METHOD
    this.showAge = function() {
        console.log(this.age);
    }
}

// INSTANTIATE INSTANCE OF OBJECT
var x = new myObj('Fred',25);

// ADD METHOD TO OBJECT THROUGH PROTOTYPE
myObj.prototype.showName = function(){
	console.log(this.name);
}

// CALL METHODS TO DEMONSTRATE THEY WORK
x.showAge();
x.showName();

// CREATE A DYNAMIC METHOD BOUND TO THIS INSTANCE ONLY
x.showNameandAge = function() {
   console.log(this.name + ' is ' + this.age + ' years old');
}

// TEST THE DYNAMIC FUNCTION
x.showNameandAge();

// CREATE SECOND OBJECT
var y = new myObj('Peter',21);

// TEST THAT DYNAMICALLY ADDED GLOBAL METHOD WORKS
y.showName();

// SHOW THAT DYNAMICALLY ADDED INSTANCE METHOD FAILS
// ONLY AVAILABLE TO OBJECT x
y.showNameandAge();

</script>

Open in new window


Output
 25
Fred
Fred is 25 years old
Peter
TypeError: y.showNameandAge is not a function

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
userTesterAuthor Commented:
Thanks for your input chaau, Julian is correct. I have done it the way you describe, but the idea is to add the method to the main object that is inside the function.

Julian, is using prototype the only way to do this?
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Julian HansenCommented:
Julian, is using prototype the only way to do this?
Pretty much - not sure what the question is - is there a problem with doing it that way?
Julian HansenCommented:
You can read more here for reference
http://www.w3schools.com/js/js_object_prototypes.asp
userTesterAuthor Commented:
Julian,

No problem using prototype, but because I am still getting familiar with JavaScript functions, and the fact that they can hold objects, I want to get to know the limits of how to access and manipulate objects that are defined inside a function.

I assume that using prototype is just one way of adding methods to an object that is defined inside a function, thus the need to know if there are alternate ways to accomplish the same thing?
Julian HansenCommented:
That is the standard (best supported) way of doing it.
userTesterAuthor Commented:
This is the correct way of doing it, thanks.
Julian HansenCommented:
You are welcome
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.