diff --git a/prototype.js b/prototype.js index 06b3a60..50f4685 100644 --- a/prototype.js +++ b/prototype.js @@ -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; } } }); diff --git a/tests/create.js b/tests/create.js index 8fb03f6..c62ff8d 100644 --- a/tests/create.js +++ b/tests/create.js @@ -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");