Skip to content

Instantly share code, notes, and snippets.

@kn9ts
Created April 28, 2015 17:13
Show Gist options
  • Save kn9ts/f5cae5014becb54e5121 to your computer and use it in GitHub Desktop.
Save kn9ts/f5cae5014becb54e5121 to your computer and use it in GitHub Desktop.
How jQuery's $.extend function works, a way for object to inherit other objects properties and functions
function extend(target_object) {
// Look for additional parameters in this function
// eg. extend(target_object, ....a,b,c,d)
if (!arguments[1]) {
return;
}
// If any extra arguments, which are objects
// loop through them
for (var index = 1; index < arguments.length; index++) {
// get the object
var object = arguments[index];
for (var prop in object) {
// Make sure the object property does not exist in the target_object object
// Also that the property is the objects own property and not the default ones
// inherited through JavaScript itself
if (!target_object[prop] && object.hasOwnProperty(prop)) {
// now pass the properties
target_object[prop] = object[prop]
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment