Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
fix($q): Use extend to avoid overwriting prototype
Browse files Browse the repository at this point in the history
Use `extend` on `Promise.prototype` and `Deferred.prototype`, to avoid having to
manually set `constructor` on the overwritten prototypes.

Closes #10697
  • Loading branch information
bluepnume authored and lgalfaso committed Jul 18, 2015
1 parent 8ed6829 commit 3abb3fe
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
14 changes: 14 additions & 0 deletions lib/promises-aplus/promises-aplus-test-adapter.js
Expand Up @@ -11,6 +11,20 @@ var minErr = function minErr (module, constructor) {
};
};

var extend = function extend(dst) {
for (var i = 1, ii = arguments.length; i < ii; i++) {
var obj = arguments[i];
if (obj) {
var keys = Object.keys(obj);
for (var j = 0, jj = keys.length; j < jj; j++) {
var key = keys[j];
dst[key] = obj[key];
}
}
}
return dst;
};

var $q = qFactory(process.nextTick, function noopExceptionHandler() {});

exports.resolved = $q.resolve;
Expand Down
8 changes: 4 additions & 4 deletions src/ng/q.js
Expand Up @@ -272,7 +272,7 @@ function qFactory(nextTick, exceptionHandler) {
this.$$state = { status: 0 };
}

Promise.prototype = {
extend(Promise.prototype, {
then: function(onFulfilled, onRejected, progressBack) {
var result = new Deferred();

Expand All @@ -294,7 +294,7 @@ function qFactory(nextTick, exceptionHandler) {
return handleCallback(error, false, callback);
}, progressBack);
}
};
});

//Faster, more basic than angular.bind http://jsperf.com/angular-bind-vs-custom-vs-native
function simpleBind(context, fn) {
Expand Down Expand Up @@ -341,7 +341,7 @@ function qFactory(nextTick, exceptionHandler) {
this.notify = simpleBind(this, this.notify);
}

Deferred.prototype = {
extend(Deferred.prototype, {
resolve: function(val) {
if (this.promise.$$state.status) return;
if (val === this.promise) {
Expand Down Expand Up @@ -404,7 +404,7 @@ function qFactory(nextTick, exceptionHandler) {
});
}
}
};
});

/**
* @ngdoc method
Expand Down

0 comments on commit 3abb3fe

Please sign in to comment.