Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Remove support for new in bind

Also normalize the use of thisp as a variable name.
  • Loading branch information...
commit 71df2ef73bc0b40b2abb494bfd984250d907b821 1 parent 6be15b9
@kriskowal authored
Showing with 7 additions and 109 deletions.
  1. +7 −25 q.js
  2. +0 −84 test/bind.js
View
32 q.js
@@ -788,7 +788,7 @@ exports.invoke = function (value, name) {
/**
* Applies the promised function in a future turn.
* @param object promise or immediate reference for target function
- * @param context the context object (this) for the call
+ * @param thisp the `this` object for the call
* @param args array of application arguments
*/
var apply = exports.apply = dispatcher("apply");
@@ -803,13 +803,13 @@ var fapply = exports.fapply = dispatcher("fapply");
/**
* Calls the promised function in a future turn.
* @param object promise or immediate reference for target function
- * @param context the context object (this) for the call
+ * @param thisp the `this` object for the call
* @param ...args array of application arguments
*/
exports.call = call;
-function call(value, context) {
+function call(value, thisp) {
var args = slice.call(arguments, 2);
- return apply(value, context, args);
+ return apply(value, thisp, args);
}
/**
@@ -827,33 +827,15 @@ function fcall(value) {
* Binds the promised function, transforming return values into a fulfilled
* promise and thrown errors into a rejected one.
* @param object promise or immediate reference for target function
- * @param context the context object (this) for the call
+ * @param thisp the `this` object for the call
* @param ...args array of application arguments
*/
exports.bind = bind;
-function bind(value, context) {
+function bind(value, thisp) {
var args = slice.call(arguments, 2);
-
return function bound() {
var allArgs = args.concat(slice.call(arguments));
-
- if (this instanceof bound) {
- var F = function () { };
- F.prototype = value.prototype;
- var self = new F();
-
- var result = apply(value, self, allArgs);
-
- return result.then(function (fulfilledValue) {
- // if Object.isObject(fulfilledValue)
- if (Object(fulfilledValue) === fulfilledValue) {
- return fulfilledValue;
- }
- return self;
- });
- } else {
- return apply(value, context, allArgs);
- }
+ return apply(value, thisp, allArgs);
};
}
View
84 test/bind.js
@@ -112,90 +112,6 @@ exports['test uses existing context if none given'] = function (ASSERT, done) {
.fin(done);
};
-exports['test invoking with new'] = function (ASSERT, done) {
- function Point(x, y) {
- this.x = x;
- this.y = y;
- }
-
- var BoundPoint = Q.bind(Point, null, 1);
-
- (new BoundPoint(2))
- .then(function (point) {
- ASSERT.deepEqual(point, { x: 1, y: 2 }, "fulfilled with constructed");
- })
- .fail(function (reason) {
- ASSERT.ok(false, reason);
- }).
- fin(done);
-};
-
-exports['test returns correctly when new\'ed'] = function (ASSERT, done) {
- var returned = {};
-
- var bound = Q.bind(function () { return returned; });
-
- (new bound())
- .then(function (val) {
- ASSERT.strictEqual(val, returned, "fulfilled with correct value");
- })
- .fail(function (reason) {
- ASSERT.ok(false, reason);
- })
- .fin(done);
-};
-
-exports['test throwing when new\'ed'] = function (ASSERT, done) {
- var throwMe = new Error("boo!");
-
- var bound = Q.bind(function () {
- throw throwMe;
- });
-
- (new bound())
- .then(function () {
- ASSERT.ok(false, "should not be fulfilled");
- })
- .fail(function (reason) {
- ASSERT.strictEqual(reason, throwMe, "rejected with correct reason");
- })
- .fin(done);
-};
-
-exports['test returns only objects when new\'ed'] = function (ASSERT, done) {
- var bound = Q.bind(function () {
- this.x = 1;
-
- return 5;
- });
-
- (new bound())
- .then(function (val) {
- ASSERT.deepEqual(val, { x: 1 }, "fulfilled with object not primitive");
- })
- .fail(function (reason) {
- ASSERT.ok(false, reason);
- })
- .fin(done);
-};
-
-exports['test returns only objects when new\'ed 2'] = function (ASSERT, done) {
- var bound = Q.bind(function () {
- this.x = 1;
-
- return Q.resolve(5);
- });
-
- (new bound())
- .then(function (val) {
- ASSERT.deepEqual(val, { x: 1 }, "fulfilled with object not primitive");
- })
- .fail(function (reason) {
- ASSERT.ok(false, reason);
- })
- .fin(done);
-};
-
if (module == require.main) {
require('test').run(exports);
}
Please sign in to comment.
Something went wrong with that request. Please try again.