jsFiddle Demo
There are several options. A highly used approach is prototypes. A prototype will extend the object created with the functions defined on the prototype if the new
keyword is used. You can take advantage of this to expose functions.
var video = function() {
if( !(this instanceof video) ){//ensure that we always work with an instance of video
return new video();
}
this.name = "Name of Video";
this.desc = "Short Description of Video";
this.long = "Long Description of Video";
};
video.prototype.metadata = function(){
return {
name : this.name,
shortDescription : this.desc,
longDescription : this.long
};
};
Now the options, it can be called directly:
console.log(video().metadata());
It can be used as a function call and then referenced
var v = video();
console.log(v.metadata());
Or it can be explicitly instantiated and then referenced
var vid = new video();
console.log(vid.metadata());
This ensures that basically all uses of the function end up with the same functionality.