Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct class naming for the inspector #33

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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

Expand Down
22 changes: 13 additions & 9 deletions src/p.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions test/p.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});

});


});