From 8f89460c4601c57bbad225f51c1e2744cc206e53 Mon Sep 17 00:00:00 2001 From: Vsevolod Novikov Date: Sun, 4 Jan 2015 23:24:49 +0300 Subject: [PATCH 1/2] Correct class naming for the inspector --- src/p.js | 22 +++++++++++++--------- test/p.test.js | 18 ++++++++++++++++++ 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/src/p.js b/src/p.js index 6805b5f..a394568 100644 --- a/src/p.js +++ b/src/p.js @@ -6,26 +6,30 @@ var P = (function(prototype, ownProperty, undefined) { _superclass = Object; } + var name = 'Bare'; + if(typeof definition === 'function') { + name = definition.name; + } else if(definition && definition['__classname__']) { + name = definition['__classname__']; + } + // C is the class to be returned. // // When called, creates and initializes an instance of C, unless // `this` is already an instance of C, then just initializes `this`; // either way, returns the instance of C that was initialized. // - // TODO: the Chrome inspector shows all created objects as `C` - // rather than `Object`. Setting the .name property seems to - // have no effect. Is there a way to override this behavior? - function C() { - var self = this instanceof C ? this : new Bare; - self.init.apply(self, arguments); - return self; - } + var C = null; + eval("C = function "+name+"() {var self = this instanceof C ? this : new Bare;self.init.apply(self, arguments);return self;}"); // C.Bare is a class with a noop constructor. Its prototype will be // the same as C, so that instances of C.Bare are instances of C. // `new MyClass.Bare` then creates new instances of C without // calling .init(). - function Bare() {} + + var Bare = null; + eval("Bare = function "+name+"(){}"); + C.Bare = Bare; // Extend the prototype chain: first use Bare to create an diff --git a/test/p.test.js b/test/p.test.js index c6479cf..7cf1126 100644 --- a/test/p.test.js +++ b/test/p.test.js @@ -254,4 +254,22 @@ describe('P', function() { assert.equal(C.extend(mixin1).extend(mixin2)().foo(), 2); }); }); + + describe('naming to show in the inspector', function() { + // TODO: how to check Chrome inspector behaviour correctly? + it('name from the descriptor function', function() { + var C = P(function X() {}); + assert.equal(C.name, 'X'); + assert.equal(C.Bare.name, 'X'); + }); + + it('name from the descriptor __classname__ attribute', function() { + var C = P({__classname__:'X'}); + assert.equal(C.name, 'X'); + assert.equal(C.Bare.name, 'X'); + }); + + }); + + }); From 4fa932d8f10c2e51a1518e9a17c71f410abcc322 Mon Sep 17 00:00:00 2001 From: Vsevolod Novikov Date: Sun, 4 Jan 2015 23:33:51 +0300 Subject: [PATCH 2/2] Correct class naming for the inspector - some documenting --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 0341054..0bd9533 100644 --- a/README.md +++ b/README.md @@ -11,22 +11,22 @@ Okay. ``` js // adapted from coffeescript.org // P.js exposes the `P` variable -var Animal = P(function(animal) { +var Animal = P(function Animal(animal) { animal.init = function(name) { this.name = name; }; animal.move = function(meters) { - console.log(this.name+" moved "+meters+"m."); + console.log(this.name+" moved "+meters+"m.",this); } }); -var Snake = P(Animal, function(snake, animal) { +var Snake = P(Animal, function Snake(snake, animal) { snake.move = function() { console.log("Slithering..."); animal.move.call(this, 5); }; }); -var Horse = P(Animal, function(horse, animal) { +var Horse = P(Animal, function Horse(horse, animal) { horse.move = function() { console.log("Galloping..."); animal.move.call(this, 45); @@ -102,9 +102,9 @@ P(MySuperclass, function(proto, super, class, superclass) { // for shorthand, you can pass an object in lieu of the function argument, // but you lose the niceness of super and private methods. -P({ init: function(a) { this.thing = a } }); +P({ init: function(a) { this.thing = a }, __classname__:'MyClass' }); -MyClass = P(function(p) { p.init = function(a, b) { console.log("init!", a, b) }; }); +MyClass = P(function MyClass(p) { p.init = function(a, b) { console.log("init!", a, b) }; }); // instantiate objects by calling the class as a function MyClass(1, 2) // => init!, 1, 2