-
-
Save jergason/1848193 to your computer and use it in GitHub Desktop.
A pattern (pun intended) - strict, non-strict mode, object.create
This file contains hidden or 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
// Strict mode and non-strict mode | |
(function () { | |
"use strict"; | |
function Foo(a, b, c) { | |
if (!this instanceof Foo) { | |
return new Foo(a, b, c); | |
} | |
this.a = a; | |
this.b = b; | |
this.c = c; | |
} | |
Foo.create = function () { | |
new Foo(); | |
}; | |
Foo.create(1, 2, 3); // works | |
new Foo(1, 2, 3); // works | |
Foo(1, 2, 3); // works | |
}()); | |
// Object.create | |
(function () { | |
"use strict"; | |
var foo | |
; | |
function Foo() { | |
} | |
foo = Object.create(Foo, { a: 'bar', b: 'baz', c: 'corge' }); | |
foo.a; // bar | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment