I don't understand why my code doesn't want to run


#1


Hello Guys, I just wanted to try this out and didn't know how it works....

function Hero (name, knownas, age, weakness, bestie, loveintrest, villian) {
    this.name = name;
    this.knownas = knownas;
    this.age = age;
    this.weakness = weakness;
    this.bestie = bestie;
    this.loveintrest = loveintrest;
    this.villian = villian
    sayHero: function(hero) {
        console.log("My name is" + this.name + "and am the" + this.knowas + "I am" + this.age + "years old, my bestie is" + this.bestie);
                        }
                    };

var barry = new Hero('Barry Allen', 'Fastest man alive', 27, 'none', "Hal Jordan", "Iris West", "Reverse Flash");

barry.sayHero();


gives this error message...
Exception: SyntaxError: function statement requires a name...... which function should I give a name please 

Thanks a lot.


#2

Your console.log does no have the parameters you inputted when creating your object.

And here, your function is called,

but you call it like,


#3

Made corrections, still doesn't print it out? Where am I getting it wrong and how can it be corrected please?


#4

Try this, :slight_smile:

function Hero(name, knownas, age, weakness, bestie, loveintrest, villian) {
    this.name = name;
    this.knownas = knownas;
    this.age = age;
    this.weakness = weakness;
    this.bestie = bestie;
    this.lovinterest = loveintrest;
    this.villian = villian;
}


Hero.prototype.sayHero= function() {
    console.log("My name is " + this.name + " and I'm the " + this.knownas + " I am " + this.age + " years old.");
    };

var barry = new Hero('Barry Allen', 'Fastest man alive', 27, 'none', "Hal Jordan", "Iris West", "Reverse Flash");



barry.sayHero();

#5

Thanks a lot!!! I was actually using the code that was giving in the chapter, I don't know why it worked though
maybe because of it's a literal notation

var james = {
job: "programmer",
married: false,
sayJob: function() {
console.log("Hi, I work as a "+this.job);
}
};

// james' first job
james.sayJob();

// change james' job to "super programmer" here
james.job = "super programmer";

// james' second job
james.sayJob()

used "sayJob: function" instead of what you gave me...

Can you explain why it worked please?


#6

why what code worked this one?


#7

sayJb

It worked unlike when I used it for constructor notation ..


#8

Well look here,

var james = {
    job: "programmer",
    married: false,
    sayJob: function() {
        console.log("Hi, I work as a "+this.job);
    }
};

// james' first job
james.sayJob();

// change james' job to "super programmer" here
james.job = "super programmer";

// james' second job
james.sayJob()

james is created as a literal notation object not a constructor like yours. And adding attributes is more straightforward in literal notation than it is when dealing with constructors. :slight_smile:


#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.