Reputation: 318
I'm having a problem with my function syntax, when I call the function and give its two parameters strings the output is always undefined. I don't understand why its doing this, I was also wondering if this could go on the code review part of stack exchange since its bug free. Any answers would be greatly appreciated!
function sm(name, location){
console.log("Hello " + this.name + " from " + this.location);
}
sm("josh", "hawaii");
Upvotes: 1
Views: 252
Reputation: 441
You don't need to use this
here. Just name
and location
is enough to refer the variable passed as the function parameter.
function sm(name, location){
console.log("Hello " + name + " from " + location);
}
sm("josh", "hawaii");
Upvotes: 0
Reputation: 41
function sm(name, location){
console.log("Hello " + name + " from " + location);
}
sm("josh", "hawaii");
Hope it helps you
Upvotes: 0
Reputation: 32145
The function
parameters are not properties of the function so you can't access them with this
keyword.
And in your function this
will refer to the global window
object so the compiler will look for these properties inside the window
object and will fire an exception if they weren't defined.
Just write:
console.log("Hello " + name + " from " + location);
Demo:
function sm(name, location){
console.log("Hello " + name + " from " + location);
}
sm("josh", "hawaii");
Upvotes: 2
Reputation: 1005
No need using this
for functional parameters.
function sm(name, location){
console.log("Hello " + name + " from " + location);
}
sm("josh", "hawaii");
Upvotes: 0