Skip to content
Permalink
Browse files

fix($q): Use extend to avoid overwriting prototype

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 Jan 9, 2015
1 parent 8ed6829 commit 3abb3fefe653df2a4cb730cface0049939c18efd
Showing with 18 additions and 4 deletions.
  1. +14 −0 lib/promises-aplus/promises-aplus-test-adapter.js
  2. +4 −4 src/ng/q.js
@@ -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;
@@ -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();

@@ -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) {
@@ -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) {
@@ -404,7 +404,7 @@ function qFactory(nextTick, exceptionHandler) {
});
}
}
};
});

/**
* @ngdoc method

0 comments on commit 3abb3fe

Please sign in to comment.
You can’t perform that action at this time.