Tyler Hines
Tyler Hines

Reputation: 25

Issue Creating New Object

var Contact = function(name, number) {
    this.name = name;
    this.number = number;
}

Contact.prototype.greet = function() {
    document.getElementById("output").innerHTML = this.name + " " + this.number;
};

function createContact(newName, newNumber) {
    var newName = new Contact(newName, newNumber);
}

var tyler = new Contact("Tyler", "555-555-5555");
var mike = new Contact("mike", "555-555-5555");

createContact("steve", "555-555-5555");

alert(steve.name);


$("#addButton").click(function() {
    steve.greet();
});

I am having an issue with the above code. The issue I am having is with the createContact function. What I am trying to accomplish is to write a function that will eventually be based on user input that creates a new contact with a name and a number. I also want the name of the new Contact object to be the name of the newName, hence var newName = new Contact. Using the createContact function as it stands now does not accomplish that. I can create a new Contact object directly like I did with the "tyler" and "mike" Contacts, but not through the function.

I have tried to find the answer on my own, but have had no luck. I don't really know what I should be searching for at this point, so I'm just stuck.

Also, am I using the word "object" wrong when I say I "created a tyler object of the Contact object"?

Thanks!

Upvotes: -1

Views: 59

Answers (3)

Anonymous0day
Anonymous0day

Reputation: 3042

One solution is with this modification :

function createContact(newName, newNumber, context) {
    var ctx = context || this;
    ctx[newName] = new Contact(newName, newNumber);
    return ctx[newName];
}

var Contact = function(name, number) {
    this.name = name;
    this.number = number;
}

function createContact(newName, newNumber, context) {
  var ctx = context || this;
  ctx[newName] = new Contact(newName, newNumber);
  return ctx[newName];
}

createContact("Tyler" , "555-555-5555");
createContact("mike"  , "555-555-5555");
createContact("steve" , "555-555-5555");

alert( Tyler.name + ' | ' + mike.name + ' | ' + steve.name );

Upvotes: 0

Sandeep Nayak
Sandeep Nayak

Reputation: 4757

How about like returning the instance from within createContact method.

var Contact = function(name, number) {
  this.name = name;
  this.number = number;
}

Contact.prototype.greet = function() {
  document.getElementById("output").innerHTML = this.name + " " + this.number;
};

function createContact(newName, newNumber) {
  return new Contact(newName, newNumber); // I return the instance here
}

var tyler = new Contact("Tyler", "555-555-5555");
var mike = new Contact("mike", "555-555-5555");

var steve = createContact("steve", "555-555-5555");

alert(steve.name);

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1074385

That would be a very unusual function. You can do it, though, by having an object that all of these will live in (which can be the global object if you really want it to be, but I wouldn't recommend it), then:

function createContact(newName, newNumber) {
    theObject[newName] = new Contact(newName, newNumber);
}

So for instance:

var contacts = {};

function createContact(newName, newNumber) {
    contacts[newName] = new Contact(newName, newNumber);
}

createContact("steve", "555-555-5555");
alert(contacts.steve.name);

Or using globals, via the window global:

function createContact(newName, newNumber) {
    window[newName] = new Contact(newName, newNumber);
}

createContact("steve", "555-555-5555");
alert(steve.name);

Again, though, I'd discourage you from using globals. The global namespace is already very crowded.


Of course, again, this is a very unusual function. The normal thing would be to just do it yourself:

var steve = createContact("steve", "555-555-5555");

or even

var steve = new Contact("steve", "555-555-5555");

But you said you knew how to do that, so...

Upvotes: 1

Related Questions