Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fix `new` usage in README examples #1

Merged
merged 1 commit into from

2 participants

@isaacs

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 Raynos merged commit 8b41aad into Raynos:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 15, 2012
  1. @isaacs

    Fix `new` usage in README examples

    isaacs authored
    
        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.
This page is out of date. Refresh to see the latest.
Showing with 2 additions and 2 deletions.
  1. +2 −2 README.md
View
4 README.md
@@ -9,7 +9,7 @@ Serve static files
uri: path.join(__dirname, "static")
})
, http = require("http")
- , router = new require("routes").Router()
+ , router = new (require("routes").Router)()
router.addRoute("/static/*?", $static)
@@ -28,7 +28,7 @@ Serve static files
var path = require("path")
, http = require("http")
- , router = new require("routes").Router()
+ , router = new (require("routes").Router)()
, stylus = require("stylus")
, nib = require("nib")
, routilStatic = require("routil-static")
Something went wrong with that request. Please try again.