0

my apologies if this question has been answered in previous post. I looked but found nothing like it. I am wondering if anyone knows of a way that I can add a key to a resulting object from a factory function. Meaning, outside of the factory function I want to later add a key versus going back to the factory function itself and changing what the out put should be.

/*below you will see where I have tried to add the key from the outside using the function name as the object name similiar to how one would with a standard object but the code below will return undefinded, my guess is because the object has no name, but rather is simply returned.*/

function createCharacter(name, nick, race, origin, attack, defense){
	return {
		name: name,
		nickname: nick,
		race: race,
		origin: origin,
		attack: attack,
		defense: defense,
		describe: function(){
			console.log(`${this.name} is a ${this.race} from ${this.origin}`);
		}
}

createCharacter.weapon = weapon;

2 Answers 2

1

You need to create an instance of the function using new createCharacter():

/*below you will see where I have tried to add the key from the outside using the function name as the object name similiar to how one would with a standard object but the code below will return undefinded, my guess is because the object has no name, but rather is simply returned.*/

function createCharacter(name, nick, race, origin, attack, defense){
	return {
		name: name,
		nickname: nick,
		race: race,
		origin: origin,
		attack: attack,
		defense: defense,
		describe: function(){
			console.log("${this.name} is a ${this.race} from ${this.origin}"+weapon);
		}
    }
}

x = new createCharacter()
x.weapon = "sword";
console.log(x.weapon);

Sign up to request clarification or add additional context in comments.

Comments

0

If you continue use createCharacter function,get a Character instance like this:

function createCharacter(name, nick, race, origin, attack, defense){
    return {
        name: name,
        nickname: nick,
        race: race,
        origin: origin,
        attack: attack,
        defense: defense,
        describe: function(){
            console.log(`${this.name} is a ${this.race} from ${this.origin}`);
        }
   }
}
//just directly invoke factory function,which will return a Character instance
var oneCharacter = createCharacter();
oneCharacter.weapon = "weapon";

another way is use constuctor,like this:

function Character(name, nick, race, origin, attack, defense){
    this.name = name;
    this.nick = nick;
    this.race = race;
    this.origin = origin;
    this.attack = attack;
    this.defense = defense;
    this.describe = function(){
            console.log(`${this.name} is a ${this.race} from ${this.origin}`);
        }
}
//use keyword "new" get a Character instance 
var oneCharacter = new Character();
oneCharacter.weapon = "weapon";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.