Skip to content

Commit

Permalink
fix to reconstruct prototypes in browsers without Object.create()
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Jun 16, 2012
1 parent 6b78600 commit 0cb3f34
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
18 changes: 12 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ function walk (root, cb, immutable) {
if (stopHere) keepGoing = false;
},
remove : function (stopHere) {
if (Array_isArray(state.parent.node)) {
if (isArray(state.parent.node)) {
state.parent.node.splice(state.key, 1);
}
else {
Expand Down Expand Up @@ -221,7 +221,7 @@ function copy (src) {
if (typeof src === 'object' && src !== null) {
var dst;

if (Array_isArray(src)) {
if (isArray(src)) {
dst = [];
}
else if (isDate(src)) {
Expand All @@ -245,12 +245,18 @@ function copy (src) {
else if (Object.create && Object.getPrototypeOf) {
dst = Object.create(Object.getPrototypeOf(src));
}
else if (src.__proto__ || src.constructor.prototype) {
var proto = src.__proto__ || src.constructor.prototype || {};
else if (src.constructor === Object) {
dst = {};
}
else {
var proto =
(src.constructor && src.constructor.prototype)
|| src.__proto__
|| {}
;
var T = function () {};
T.prototype = proto;
dst = new T;
if (!dst.__proto__) dst.__proto__ = proto;
}

forEach(Object_keys(src), function (key) {
Expand All @@ -275,7 +281,7 @@ function isBoolean (obj) { return toS(obj) === '[object Boolean]' }
function isNumber (obj) { return toS(obj) === '[object Number]' }
function isString (obj) { return toS(obj) === '[object String]' }

var Array_isArray = Array.isArray || function isArray (xs) {
var isArray = Array.isArray || function isArray (xs) {
return Object.prototype.toString.call(xs) === '[object Array]';
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name" : "traverse",
"version" : "0.6.1",
"version" : "0.6.2",
"description" : "Traverse and transform objects by visiting every node on a recursive walk",
"author" : "James Halliday",
"license" : "MIT/X11",
Expand Down

0 comments on commit 0cb3f34

Please sign in to comment.