Skip to content

Commit

Permalink
feat(prototype): allow prototype value to be a function
Browse files Browse the repository at this point in the history
By supplying a function as the value of `prototype` you can now alter the
prototype chain.

BREAKING CHANGE: We've retired `proto` as it can be better implemented
with a callback.
  • Loading branch information
mikaelkaron committed Oct 16, 2016
1 parent 2d95753 commit 1f99354
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 13 deletions.
16 changes: 8 additions & 8 deletions prototype.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@
}));
}
})([], this, function () {
function proto() {};
var toString = Object.prototype.toString;

return function (result, data) {
if (data.key === "proto") {
proto.prototype = data.value;
result.prototype = new proto();
}
else if (data.key === "prototype") {
result.prototype = data.value;
var value = data.value;

if (data.key === "prototype") {
result.prototype = toString.call(value) === "[object Function]"
? value.call(this, result)
: value;
}
else {
result.prototype[data.key] = data.value;
result.prototype[data.key] = value;
}
}
});
46 changes: 41 additions & 5 deletions tests/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,23 +133,59 @@

QUnit.module("mu-create/create#property");

QUnit.test("proto instance", function (assert) {
QUnit.test("prototype instance", function (assert) {
assert.expect(1);

var p = {};
var C = create(proto)({
"proto": p
"prototype": p
});

assert.strictEqual(C.prototype.__proto__, p, "instance is equal");
assert.strictEqual(C.prototype, p, "instance is equal");
});

QUnit.test("prototype instance", function (assert) {
QUnit.test("prototype function", function (assert) {
assert.expect(1);

var C = create(proto)({
"prototype": function() {
assert.ok(true, "callback called");
}
});
});

QUnit.test("prototype function arguments", function (assert) {
assert.expect(1);

var p;
var C = create(proto)({
"prototype": function(c) {
return p = c.prototype;
}
});

assert.strictEqual(p, C.prototype, "p matches C.prototype");
});

QUnit.test("prototype function scope", function (assert) {
assert.expect(1);

var s = {};
var C = create(proto).call(s, {
"prototype": function() {
assert.strictEqual(this, s, "scope matches");
}
});
});

QUnit.test("prototype function return", function (assert) {
assert.expect(1);

var p = {};
var C = create(proto)({
"prototype": p
"prototype": function() {
return p;
}
});

assert.strictEqual(C.prototype, p, "instance is equal");
Expand Down

0 comments on commit 1f99354

Please sign in to comment.