Skip to content

Instantly share code, notes, and snippets.

@samarpanda
Last active December 24, 2015 16:09
Show Gist options
  • Save samarpanda/6825972 to your computer and use it in GitHub Desktop.
Save samarpanda/6825972 to your computer and use it in GitHub Desktop.
My pretty javascript functions.
/**
* 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();
@samarpanda
Copy link
Author

ValueObject = function(value) {
  this.value = value;
}

$.extend(ValueObject.prototype, {
  get: function() {
    return this.value;
  }
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment