Skip to content

Instantly share code, notes, and snippets.

@twolfson
Created November 25, 2013 10:47
Show Gist options
  • Save twolfson/7639547 to your computer and use it in GitHub Desktop.
Save twolfson/7639547 to your computer and use it in GitHub Desktop.
JS pitfall solutions via a compile-to-JS langauge

JavaScript is quirky but that is why I like it. Unfortunately, there is a lot of problems for amateur developers if they don't take the time to learn the language.

These solutions are transcribed from past notes on stepping over quirky pitfalls


ProperScript (ClassicScript) removes first argument and sets it as var on first line

fn (self, b, c) {

}

TO

fn (b, c) {
  var self = this;
}

On instantiation, all methods must be bound to this

fn A() {
  this.b = this.b.bind(this);
}

Coffeescript harder to read due to lack of parentheses and braces to distinguish sections

@twolfson
Copy link
Author

The other reason for switching that people usually state is length of function. This choses to employ fn and side-step passing context around via fat arrows.

@twolfson
Copy link
Author

A note from long ago:


  • Allow fn for functions
  • Auto-name anonymous inline fn's
  • Closure everything to block-level scoping
  • self as first param for all classical inheritances
  • Support classical extension
  • Equality stays same. I have never seen anyone shoot themself in the foot with this.
    • This is probably the most pragmatically spread best practice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment