Assume you have a function, and that function has a param (called 'config' herein) which may be a string or an object. Inside your function, you check for the type and if it's a string, you want to make this strings value a property of your object.
My first, fast approach was this one:
var url = config;
config = {};
config.videoLink = url;
My second approach looked like this:
var url = config;
config = {videoLink: url};
I thought about what's happening in those statements and realized that the temporary variable 'url' is not needed, because javascript is executing the assignement from right to left, means I can override 'config' with an object that keeps the original value of config as a property. See this third & final approach:
config = {videoHash: config};
So Javascript builds the object, assigns the value of config (string) to videoHash and assigns this object to config, which now is a object as desired.
Keine Kommentare:
Kommentar veröffentlichen