Skip to content

Instantly share code, notes, and snippets.

@leandro
Last active September 8, 2016 14:49
Show Gist options
  • Save leandro/e8b40aaf4727883981255bac62df2d64 to your computer and use it in GitHub Desktop.
Save leandro/e8b40aaf4727883981255bac62df2d64 to your computer and use it in GitHub Desktop.
Lea Verou's proposal regarding URL class in https://leaverou.github.io/jsux/
!function() {
var myURL = function(str) {
this.str = str;
this.toString = function() { return str; };
}
function createFromString(urlString) {
return new myURL(urlString);
}
function createFromURL(url) {
return new myURL(url.str);
}
function createFromLocation(extraURI) {
var urlString = window.location.href + (extraURI || "");
return new myURL(urlString);
}
var myURLFactory = {
createFromString: createFromString,
createFromURL: createFromURL,
createFromLocation: createFromLocation
};
window.myURL = myURL;
window.myURLFactory = myURLFactory;
}();
// > myURLFactory.createFromURL(new myURL("http://abre.ai/")) + "ae"
// < "http://abre.ai/ae"
// > myURLFactory.createFromString("http://abre.ai/") + "ae"
// < "http://abre.ai/ae"
// > myURLFactory.createFromLocation("ae") + "opa"
// < "http://www.phpied.com/3-ways-to-define-a-javascript-class/aeopa"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment