Created
December 16, 2010 12:47
-
-
Save tobiashm/743360 to your computer and use it in GitHub Desktop.
JavaScript `new` operator return an object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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