Javascript extend objects in variable
I have a function that contains messages for form field validation. However I would like to have default messages but also have the possibility to add custom messages later on from a different function (on the fly). I have tried something using php and extending a class and adding more messages and than building the javasc开发者_Go百科ript but I'm not really happy doing it this way.
function msg_text(fieldV){
msg = {
"required":{
"alertText":"* This field is required"
},
"length":{
"alertText":"* minimum 6 characters "
},
"numeric":{
"alertText":"numbers and * only<br />minimum 3 characters"
},
"email":{
"alertText":"* Invalid email address"
},
"no_space":{
"alertText":"* Is Required <br /> * Space not allowed"
}
}
return msg[fieldV].alertText; //returns alert message
}
var my{};
function createFunctions()
{
var msg = {/*messages*/};
my.getMessage = function(key)
{
return msg[key];
}
my.addMessage = function(key, message)
{
msg[key] = message
}
my.deleteMessage = function(key)
{
delete msg[key];
}
}
精彩评论