Last active
December 24, 2015 16:09
-
-
Save samarpanda/6825972 to your computer and use it in GitHub Desktop.
My pretty javascript functions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Extends Object | |
*/ | |
Object.prototype.extends = function(extension) { | |
var hasOwnProperty = Object.hasOwnProperty(), | |
object = Object.create(this); | |
for(var property in extension){ | |
if(hasOwnProperty.call(extension, property) || typeof object[property] === "undefined") | |
object[property] = extension[property]; | |
} | |
return object; | |
}; | |
/** | |
* Getter function for any object | |
*/ | |
function get(object, property) { | |
if(!Object.hasOwnProperty.call(object, property)){ | |
var prototype = Object.getPrototypeOf(object); | |
if(prototype) | |
return get(prototype, property); | |
} else { | |
return object[property]; | |
} | |
} | |
/** | |
* Multiple inheritance | |
* Cloning / Concatenative inheritance | |
*/ | |
Object.prototype.extend = function(){ | |
var hasOwnProperty = Object.hasOwnProperty, | |
object = Object.create(this), | |
length = arguments.length, | |
index = length, | |
extension; | |
while(index){ | |
extension = arguments[length - (index--)]; | |
for(var property in extension){ | |
if(hasOwnProperty.call(extension, property) || typeof object[property] === "undefined") | |
object[property] = extension[property]; | |
} | |
} | |
return object; | |
}; | |
/** | |
* Simple ajax implementation | |
*/ | |
var xhrObject = new XMLHttpRequest(); | |
xhrObject.onreadystatechange = function(){ | |
if(xhrObject.readyState === 4) { | |
if(xhrObject.status === 200 || xhrObject.status === 304){ | |
console.log(xhrObject.responseText); | |
} | |
} | |
}; | |
xhrObject.open( | |
"GET", | |
"http://samarpanda.com/sample/", | |
true | |
); | |
xhrObject.send(); |
Author
samarpanda
commented
Oct 9, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment