Skip to content

Commit ba7b8f1

Browse files
committed
removing hokum from _.bind
1 parent 173a7fa commit ba7b8f1

File tree

2 files changed

+4
-43
lines changed

2 files changed

+4
-43
lines changed

test/functions.js

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,15 @@
2828
func = _.bind(func, this, 'hello', 'moe', 'curly');
2929
equal(func(), 'hello: moe curly', 'the function was partially applied in advance and can accept multiple arguments');
3030

31-
func = function(context, message) { equal(this, context, message); };
32-
_.bind(func, 0, 0, 'can bind a function to `0`')();
33-
_.bind(func, '', '', 'can bind a function to an empty string')();
34-
_.bind(func, false, false, 'can bind a function to `false`')();
35-
3631
// These tests are only meaningful when using a browser without a native bind function
3732
// To test this with a modern browser, set underscore's nativeBind to undefined
3833
var F = function () { return this; };
3934
var boundf = _.bind(F, {hello: 'moe curly'});
4035
var Boundf = boundf; // make eslint happy.
4136
var newBoundf = new Boundf();
42-
equal(newBoundf.hello, undefined, 'function should not be bound to the context, to comply with ECMAScript 5');
37+
equal(newBoundf.hello, 'moe curly', 'function should not be bound to the context, because this ain\'t ECMA5');
4338
equal(boundf().hello, 'moe curly', "When called without the new operator, it's OK to be bound to the context");
44-
ok(newBoundf instanceof F, 'a bound instance is an instance of the original function');
39+
ok(!(newBoundf instanceof F), 'a bound instance is not an instance of the original function');
4540

4641
throws(function() { _.bind('notafunction'); }, TypeError, 'throws an error when binding to a non-function');
4742
});
@@ -62,20 +57,6 @@
6257

6358
func = _.partial(function() { return typeof arguments[2]; }, _, 'b', _, 'd');
6459
equal(func('a'), 'undefined', 'unfilled placeholders are undefined');
65-
66-
// passes context
67-
function MyWidget(name, options) {
68-
this.name = name;
69-
this.options = options;
70-
}
71-
MyWidget.prototype.get = function() {
72-
return this.name;
73-
};
74-
var MyWidgetWithCoolOpts = _.partial(MyWidget, _, {a: 1});
75-
var widget = new MyWidgetWithCoolOpts('foo');
76-
ok(widget instanceof MyWidget, 'Can partially bind a constructor');
77-
equal(widget.get(), 'foo', 'keeps prototype');
78-
deepEqual(widget.options, {a: 1});
7960
});
8061

8162
test('bindAll', function() {

underscore.js

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,6 @@
112112
};
113113
};
114114

115-
// An internal function for creating a new object that inherits from another.
116-
var baseCreate = function(prototype) {
117-
if (!_.isObject(prototype)) return {};
118-
if (nativeCreate) return nativeCreate(prototype);
119-
Ctor.prototype = prototype;
120-
var result = new Ctor;
121-
Ctor.prototype = null;
122-
return result;
123-
};
124-
125115
// Helper for collection methods to determine whether a collection
126116
// should be iterated as an array or as an object
127117
// Related: http://people.mozilla.org/~jorendorff/es6-draft.html#sec-tolength
@@ -688,16 +678,6 @@
688678
// Function (ahem) Functions
689679
// ------------------
690680

691-
// Determines whether to execute a function as a constructor
692-
// or a normal function with the provided arguments
693-
var executeBound = function(sourceFunc, boundFunc, context, callingContext, args) {
694-
if (!(callingContext instanceof boundFunc)) return sourceFunc.apply(context, args);
695-
var self = baseCreate(sourceFunc.prototype);
696-
var result = sourceFunc.apply(self, args);
697-
if (_.isObject(result)) return result;
698-
return self;
699-
};
700-
701681
// Create a function bound to a given object (assigning `this`, and arguments,
702682
// optionally). Delegates to **ECMAScript 5**'s native `Function.bind` if
703683
// available.
@@ -706,7 +686,7 @@
706686
if (!_.isFunction(func)) throw new TypeError('Bind must be called on a function');
707687
var args = slice.call(arguments, 2);
708688
return function bound() {
709-
return executeBound(func, bound, context, this, args.concat(slice.call(arguments)));
689+
return func.apply(context || this, args.concat(slice.call(arguments)));
710690
};
711691
};
712692

@@ -722,7 +702,7 @@
722702
args[i] = boundArgs[i] === _ ? arguments[position++] : boundArgs[i];
723703
}
724704
while (position < arguments.length) args.push(arguments[position++]);
725-
return executeBound(func, bound, this, this, args);
705+
return func.apply(this, args);
726706
};
727707
};
728708

0 commit comments

Comments
 (0)