Skip to content

Instantly share code, notes, and snippets.

@tobiashm
Created December 16, 2010 12:47
Show Gist options
  • Save tobiashm/743360 to your computer and use it in GitHub Desktop.
Save tobiashm/743360 to your computer and use it in GitHub Desktop.
JavaScript `new` operator return an object
function foo() { return 'foo'; }
function bar() { return foo; }
foo()
//=> "foo"
bar()
//=> function foo() { return 'foo'; }
new foo()
//=> [Object foo]
new bar()
//=> function foo() { return 'foo'; }
new foo().constructor
//=> function foo() { return 'foo'; }
new bar().constructor
//=> function Function() { [native code] }
function baz() { return { name: 'baz' }; }
baz()
//=> [Object { name: 'baz' }]
new baz()
//=> [Object { name: 'baz' }]
baz().name
//=> "baz"
new baz().name
//=> "baz"
// Notice: `null` is not considered an object is this context
function qux() { return null; }
qux()
//=> null
new qux()
//=> [Object qux]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment