javascript - Create object property with two optional conditions -
what want?
i want create object property capitalizes every word in string, optional replaces underscores spaces and/or lowercase string first. want set options 2 parameters:
first parameter true?
then replace underscores whitespace.
second parameter true?
then lowercase finish string first.
what have far working?
replace underscore space first , capitalize words:
string.prototype.capitalize = function(underscore){ homecoming (underscore ? this.replace(/\_/g, " ") : this).replace(/(?:^|\s)\s/g, function(a) { homecoming a.touppercase(); }); } var strunderscorefalse = "javascript replace_first_underderscore whitespace_false"; //replace underscore first = false console.log(strunderscorefalse.capitalize()); var strunderscoretrue = "javascript replace_first_underderscore whitespace_true"; //replace underscore first = true console.log(strunderscoretrue.capitalize(true));
fiddle
lowercase string first , capitalize words:
string.prototype.capitalize = function(lower){ homecoming (lower ? this.tolowercase() : this).replace(/(?:^|\s)\s/g, function(a) { homecoming a.touppercase(); }); } var strlcasefalse = "javascript lowercase first false"; //lowercase first = false console.log(strlcasefalse.capitalize()); var strlcasetrue = "javascript lowercase first true"; //lowercase first = true console.log(strlcasetrue.capitalize(true));
fiddle
what questions?
this first time i'm trying create object property status notice. how can bring together 2 options object property function have set 2 parameters?for example:
//replace underscore first = true , lowercase first = true console.log(str.capitalize(true , true)); //replace underscore first = false , lowercase first = true console.log(str.capitalize(false , true));
what anyway name of writing status syntax notation "?" , ":"? javascript parameters condition object-property
No comments:
Post a Comment