Skip to content

Commit

Permalink
experimental change -- comments wanted. writing out helper functions …
Browse files Browse the repository at this point in the history
…and assignments as a single line, to minimize clutter.
  • Loading branch information
jashkenas committed Sep 18, 2011
1 parent 37705e7 commit 0199515
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 41 deletions.
7 changes: 1 addition & 6 deletions lib/coffee-script/lexer.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 3 additions & 15 deletions lib/coffee-script/nodes.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 1 addition & 6 deletions lib/coffee-script/rewriter.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 2 additions & 14 deletions src/nodes.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -1801,14 +1801,7 @@ UTILITIES =
# Correctly set up a prototype chain for inheritance, including a reference
# to the superclass for `super()` calls, and copies of any static properties.
extends: -> """
function(child, parent) {
for (var key in parent) { if (#{utility 'hasProp'}.call(parent, key)) child[key] = parent[key]; }
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor;
child.__super__ = parent.prototype;
return child;
}
function(child, parent) { for (var key in parent) { if (#{utility 'hasProp'}.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; }
"""

# Create a function bound to the current value of "this".
Expand All @@ -1818,12 +1811,7 @@ UTILITIES =

# Discover if an item is in an array.
indexOf: -> """
Array.prototype.indexOf || function(item) {
for (var i = 0, l = this.length; i < l; i++) {
if (#{utility 'hasProp'}.call(this, i) && this[i] === item) return i;
}
return -1;
}
Array.prototype.indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (#{utility 'hasProp'}.call(this, i) && this[i] === item) return i; } return -1; }
"""

# Shortcuts to speed up the lookup time for native functions.
Expand Down

3 comments on commit 0199515

@michaelficarra
Copy link
Collaborator

Choose a reason for hiding this comment

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

I definitely like this change. Now let's fix #1638 by naming them, converting them to function declarations, and putting them at the end of the file.

@jashkenas
Copy link
Owner Author

Choose a reason for hiding this comment

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

Ah, but we can't for about half of them. That's where I started. Array.prototype.indexOf || function ... must be an expression, not a declaration.

@michaelficarra
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmmm....

function __indexOf() {
  return (__indexOf = Array.prototype.indexOf || function(item) {
    ...
  }).apply(this, arguments);
}

It would produce slightly larger files, but we still could meet those constraints without a runtime performance hit.

edit: some might prefer it written this way:

function __indexOf() {
  __indexOf = Array.prototype.indexOf || function(item) {
    ...
  };
  return __indexOf.apply(this, arguments);
}

Please sign in to comment.