Fix `new` usage in README examples #1

Merged
merged 1 commit into from Jul 15, 2012

Conversation

Projects
None yet
2 participants
Contributor

isaacs commented Jul 15, 2012

This:

x = new require('foo').Bar()
// is like:
x = (function () { var x = new require('foo'); return x.Bar()})()

What you really want is something like:

x = (function () { var x = require('foo').Bar; return new x()})()

so you need parens.

This works by accident, because new will return the ctor's
returned object if it's an object, and routes.Router doesn't
actually return an instance anyhow. However, if it ever did,
and it didn't use an instanceof check, then it would be a bug,
because it would poop on the global object.

@isaacs isaacs Fix `new` usage in README examples

    x = new require('foo').Bar()
    // is like:
    x = (function () { var x = new require('foo'); return x.Bar()})()

What you really want is something like:

    x = (function () { var x = require('foo').Bar; return new x()})()

so you need parens.

This works by accident, because `new` will return the ctor's
returned object if it's an object, and routes.Router doesn't
actually return an instance anyhow.  However, if it ever did,
and it didn't use an instanceof check, then it would be a bug,
because it would poop on the global object.
90493bf

Raynos merged commit 8b41aad into Raynos:master Jul 15, 2012

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