Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions lib/util/util.inherit.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ this.util = this.util || {};
* @throws {Error} if the given name has no characters matching [\w$].
*/
function createNamedFunction( name, originalFn ) {
/* jshint evil: true */
/* jshint unused: false */
var namedFn;
var evilsSeed = originalFn || EMPTY_FN;
var fnName = name.replace( /(?:(^\d+)|[^\w$])/ig, '' );
Expand All @@ -52,10 +50,13 @@ this.util = this.util || {};
throw new Error( 'Bad function name given. At least one word character or $ required.' );
}

eval( 'namedFn = function ' + fnName +
'(){ evilsSeed.apply( this, arguments ); }' );
// No, none of these functions is superfluous! Before this used to fail with a
// "ReferenceError: evilsSeed is not defined" when Shift+reloading in Firefox.
// Source: http://stackoverflow.com/a/22880379
namedFn = ( new Function( 'return function( fn ) { return function ' + fnName +
'() { return fn( this, arguments ); }; };' )() )( Function.apply.bind( evilsSeed ) );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about

namedFn = new Function( 'fn', 'return function ' + fnName +
    '() { return fn.apply( this, arguments ); };' )( evilsSeed );

?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yay for the 'fn' bit. Need to test this but looks good. Nay for the dropped bind. I think this is crucial.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it crucial?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was when I played around with my version. Since this is hard/impossible to test I'm afraid to change my version (the only one that works for sure, as far as I know) to something else.


return namedFn; // value got assigned in eval
return namedFn || evilsSeed;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did you change this? namedFn should always == true

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be sure. It may be superfluous but it doesn't hurt.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superfluous code can be very misleading

}

/**
Expand Down