Skip to content

Instantly share code, notes, and snippets.

@jergason
Forked from coolaj86/new.js
Created February 16, 2012 22:02
Show Gist options
  • Save jergason/1848193 to your computer and use it in GitHub Desktop.
Save jergason/1848193 to your computer and use it in GitHub Desktop.
A pattern (pun intended) - strict, non-strict mode, object.create
// 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