Reputation: 1130
I tried to insert a function into javascript object but I get
undefined
This is supposed to return a message into div.
I would like to do it the first way, but here is the both way I tried :
var errorMessage = {
empty: function(message){"<div class='field'><div class='csv'><span class='icon'></span><label class='manual' id='error-message'>" + message + "</label></div></div>"
}
};
console.log(errorMessage.empty("Hello"));
I also try this way
function errorMessage(message){
"<div class='field'><div class='csv'><span class='icon'></span><label class='manual' id='error-message'>" + message + "</label></div></div>"
}
console.log(errorMessage("hello"))
Upvotes: 0
Views: 109
Reputation: 68443
Your function
needs to return the value
var errorMessage = {
empty: function(message){
return "<div class='field'><div class='csv'><span class='icon'></span><label class='manual' id='error-message'>" + message + "</label></div></div>";
}
};
console.log(errorMessage.empty("Hello"));
Upvotes: 4
Reputation: 386883
You need a return
of the value literally.
The
return
statement ends function execution and specifies a value to be returned to the function caller.
var errorMessage = {
empty: function (message) {
return "<div class='field'><div class='csv'><span class='icon'></span><label class='manual' id='error-message'>" + message + "</label></div></div>";
}
};
console.log(errorMessage.empty("Hello"));
Upvotes: 6