-
-
Save geminorum/f5303a5d9beb90f0e27c7cd1522b4daf 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
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
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