Skip to content

Commit 3abb3fe

Browse files
bluepnumelgalfaso
bluepnume
authored andcommitted
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 angular#10697
1 parent 8ed6829 commit 3abb3fe

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

lib/promises-aplus/promises-aplus-test-adapter.js

+14
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,20 @@ var minErr = function minErr (module, constructor) {
1111
};
1212
};
1313

14+
var extend = function extend(dst) {
15+
for (var i = 1, ii = arguments.length; i < ii; i++) {
16+
var obj = arguments[i];
17+
if (obj) {
18+
var keys = Object.keys(obj);
19+
for (var j = 0, jj = keys.length; j < jj; j++) {
20+
var key = keys[j];
21+
dst[key] = obj[key];
22+
}
23+
}
24+
}
25+
return dst;
26+
};
27+
1428
var $q = qFactory(process.nextTick, function noopExceptionHandler() {});
1529

1630
exports.resolved = $q.resolve;

src/ng/q.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ function qFactory(nextTick, exceptionHandler) {
272272
this.$$state = { status: 0 };
273273
}
274274

275-
Promise.prototype = {
275+
extend(Promise.prototype, {
276276
then: function(onFulfilled, onRejected, progressBack) {
277277
var result = new Deferred();
278278

@@ -294,7 +294,7 @@ function qFactory(nextTick, exceptionHandler) {
294294
return handleCallback(error, false, callback);
295295
}, progressBack);
296296
}
297-
};
297+
});
298298

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

344-
Deferred.prototype = {
344+
extend(Deferred.prototype, {
345345
resolve: function(val) {
346346
if (this.promise.$$state.status) return;
347347
if (val === this.promise) {
@@ -404,7 +404,7 @@ function qFactory(nextTick, exceptionHandler) {
404404
});
405405
}
406406
}
407-
};
407+
});
408408

409409
/**
410410
* @ngdoc method

0 commit comments

Comments
 (0)