Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add Object.hasMagicLength and ES3.1 Object.create

  • Loading branch information...
commit d7f66f0469d5983a3c107c5624158e1eaf2887f3 1 parent 1385deb
kangax authored
Showing with 36 additions and 1 deletion.
  1. +36 −1 object.extensions.js
View
37 object.extensions.js
@@ -119,4 +119,39 @@ Object.isNodeList = function(object) {
Object.isPrimitive = function(o) {
return (o == null || /number|string|boolean/.test(typeof o));
-};
+};
+
+Object.hasMagicLength = function(o) {
+ if (o && typeof o == 'object' && typeof o.length == 'number' && isFinite(o.length)) {
+ var _origLength = o.length;
+ o[o.length] = '__test__';
+ var _newLength = o.length;
+ o.length = _origLength;
+ return _newLength == _origLength + 1;
+ }
+ return false;
+}
+
+/**
+ * Object.create(parent, properties) -> Object
+ *
+ * resembling the one from ES3.1
+ *
+ */
+Object.create = (function(){
+ function F(){};
+ function isObject(o) {
+ return !!o && !/undefined|boolean|string|number/.test(typeof o);
+ }
+ return function(parent, properties) {
+ if (!isObject(parent) || !isObject(properties)) {
+ throw TypeError('Object expected');
+ }
+ F.prototype = parent;
+ var object = new F;
+ for (var prop in properties) {
+ object[prop] = properties[prop];
+ }
+ return object;
+ }
+})();
Please sign in to comment.
Something went wrong with that request. Please try again.