Skip to content

Instantly share code, notes, and snippets.

@p0rsche
Created May 21, 2012 17:17
Show Gist options
  • Save p0rsche/2763377 to your computer and use it in GitHub Desktop.
Save p0rsche/2763377 to your computer and use it in GitHub Desktop.
jQuery.extend implementation in pure JS
var utils = function(){
"use strict";
var _class2type = {};
var _type = function( obj ) {
return obj == null ?
String( obj ) :
_class2type[ toString.call(obj) ] || "object";
};
var _isWindow = function( obj ) {
return obj != null && obj == obj.window;
};
var _isFunction = function(target){
return toString.call(target) === "[object Function]";
};
var _isArray = Array.isArray || function( obj ) {
return _type(obj) === "array";
};
var _isPlainObject = function( obj ) {
// Must be an Object.
// Because of IE, we also have to check the presence of the constructor property.
// Make sure that DOM nodes and window objects don't pass through, as well
if ( !obj || _type(obj) !== "object" || obj.nodeType || _isWindow( obj ) ) {
return false;
}
try {
// Not own constructor property must be Object
if ( obj.constructor &&
!hasOwn.call(obj, "constructor") &&
!hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) {
return false;
}
} catch ( e ) {
// IE8,9 Will throw exceptions on certain host objects #9897
return false;
}
// Own properties are enumerated firstly, so to speed up,
// if last one is own, then all properties are own.
var key;
for ( key in obj ) {}
return key === undefined || hasOwn.call( obj, key );
};
var _extend = function() {
var options, name, src, copy, copyIsArray, clone,
target = arguments[0] || {},
i = 1,
length = arguments.length,
deep = false;
// Handle a deep copy situation
if ( typeof target === "boolean" ) {
deep = target;
target = arguments[1] || {};
// skip the boolean and the target
i = 2;
}
// Handle case when target is a string or something (possible in deep copy)
if ( typeof target !== "object" && !_isFunction(target) ) {
target = {};
}
if ( length === i ) {
target = this;
--i;
}
for ( ; i < length; i++ ) {
// Only deal with non-null/undefined values
if ( (options = arguments[ i ]) != null ) {
// Extend the base object
for ( name in options ) {
src = target[ name ];
copy = options[ name ];
// Prevent never-ending loop
if ( target === copy ) {
continue;
}
// Recurse if we're merging plain objects or arrays
if ( deep && copy && ( _isPlainObject(copy) || (copyIsArray = _isArray(copy)) ) ) {
if ( copyIsArray ) {
copyIsArray = false;
clone = src && _isArray(src) ? src : [];
} else {
clone = src && _isPlainObject(src) ? src : {};
}
// Never move original objects, clone them
target[ name ] = _extend( deep, clone, copy );
// Don't bring in undefined values
} else if ( copy !== undefined ) {
target[ name ] = copy;
}
}
}
}
// Return the modified object
return target;
};
return {
class2type: _class2type,
type: _type,
isWindow: _isWindow,
isFunction: _isFunction,
isArray: _isArray,
isPlainObject: _isPlainObject,
extend: _extend
}
}();
//use this if you're runing under node.js
//module.exports = utils;
@borissuska
Copy link

_class2type is always empty object isn't it? Then test _class2type[ toString.call(obj) ] || "object" always returns "object". What is meaning of _type function in this implementation? It returns "null" or "object" only.

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