Is there a way to make this function not create a global variable while the body is still just an expression on one line (after return).
Seems like if it works with making a global, should be a way to do it with a local
getHashKeyValues = function(h){
return p={},h.replace(/[\|;]+([^=;]+)=([^;]*)/gi,function(s,k,v){p[k]=v}),p
}
console.log(getHashKeyValues("#/app/path|key1=value1;key2=value2"))
You can declare p as a function parameter:
getHashKeyValues = function(h,p){
return p={},h.replace(/[\|;]+([^=;]+)=([^;]*)/gi,function(s,k,v){p[k]=v}),p
};
That will make p a local variable in the function just like if you declared it with var. It is perfectly valid to call that function with only one argument, p will just be undefined until it gets assigned a value.
Declare p as a var in the function.
getHashKeyValues = function(h) {
var p = {};
h.replace(/[\|;]+([^=;]+)=([^;]*)/gi,function(s,k,v){p[k]=v});
return p;
}
And make it readable–this is an awful way to write this.
Edit Oh, I missed the "one line" requirement, which disqualifies my answer.
Even though I'm right :p
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With