Comparing version 5.0.0 to 5.1.0
exports.P = require('./build/p.commonjs').P; | ||
exports.version = JSON.parse(require('fs').readFileSync(__dirname + '/package.json')).version; | ||
exports.version = require('./package.json').version; |
{ | ||
"name": "pjs", | ||
"version": "5.0.0", | ||
"version": "5.1.0", | ||
"description": "A lightweight class system. It's just prototypes!", | ||
@@ -9,3 +9,3 @@ "keywords": ["class", "pjs", "P", "inheritance", "super"], | ||
"files": ["index.js", "src", "test", "Makefile", "package.json", "README.md", "CHANGELOG.md"], | ||
"files": ["index.js", "src", "test", "Makefile", "package.json", "README.md", "CHANGELOG.md", "build/p.commonjs.js"], | ||
"main": "index.js", | ||
@@ -17,5 +17,5 @@ "devDependencies": { | ||
"scripts": { | ||
"install": "make commonjs", | ||
"prepublish": "make commonjs", | ||
"test": "make test" | ||
} | ||
} |
@@ -46,3 +46,3 @@ [![Build Status](https://secure.travis-ci.org/jayferd/pjs.png)](http://travis-ci.org/jayferd/pjs) | ||
Most class systems for JS let you define classes by passing an object. P.js lets you pass a function instead, which allows you to closure private methods and macros. It's also 560 bytes minified (see `make report`). | ||
Most class systems for JS let you define classes by passing an object. P.js lets you pass a function instead, which allows you to closure private methods and macros. It's also <0.4kb minified (`make report`: 478). | ||
@@ -49,0 +49,0 @@ ### why doesn't pjs suck? |
58
src/p.js
var P = (function(prototype, ownProperty, undefined) { | ||
// helper functions that also help minification | ||
function isObject(o) { return typeof o === 'object'; } | ||
function isFunction(f) { return typeof f === 'function'; } | ||
// used to extend the prototypes of superclasses (which might not | ||
// have `.Bare`s) | ||
function SuperclassBare() {} | ||
return function P(_superclass /* = Object */, definition) { | ||
@@ -28,21 +20,23 @@ // handle the case where no superclass is given | ||
var self = this instanceof C ? this : new Bare; | ||
if (isFunction(self.init)) self.init.apply(self, arguments); | ||
self.init.apply(self, arguments); | ||
return self; | ||
} | ||
// C.Bare is a class with a noop constructor. Its prototype is the | ||
// same as C, so that instances of C.Bare are also instances of C. | ||
// New objects can be allocated without initialization by calling | ||
// `new MyClass.Bare`. | ||
// 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() {} | ||
C.Bare = Bare; | ||
// Set up the prototype of the new class. | ||
var _super = SuperclassBare[prototype] = _superclass[prototype]; | ||
var proto = Bare[prototype] = C[prototype] = C.p = new SuperclassBare; | ||
// Extend the prototype chain: first use Bare to create an | ||
// uninitialized instance of the superclass, then set up Bare | ||
// to create instances of this class. | ||
var _super = Bare[prototype] = _superclass[prototype]; | ||
var proto = Bare[prototype] = C[prototype] = C.p = new Bare; | ||
// other variables, as a minifier optimization | ||
var extensions; | ||
// pre-declaring the iteration variable for the loop below to save | ||
// a `var` keyword after minification | ||
var key; | ||
// set the constructor property on the prototype, for convenience | ||
@@ -54,19 +48,13 @@ proto.constructor = C; | ||
return (C.open = function(def) { | ||
extensions = {}; | ||
if (isFunction(def)) { | ||
if (typeof def === 'function') { | ||
// call the defining function with all the arguments you need | ||
// extensions captures the return value. | ||
extensions = def.call(C, proto, _super, C, _superclass); | ||
def = def.call(C, proto, _super, C, _superclass); | ||
} | ||
else if (isObject(def)) { | ||
// if you passed an object instead, we'll take it | ||
extensions = def; | ||
} | ||
// ...and extend it | ||
if (isObject(extensions)) { | ||
for (var ext in extensions) { | ||
if (ownProperty.call(extensions, ext)) { | ||
proto[ext] = extensions[ext]; | ||
if (typeof def === 'object') { | ||
for (key in def) { | ||
if (ownProperty.call(def, key)) { | ||
proto[key] = def[key]; | ||
} | ||
@@ -76,7 +64,5 @@ } | ||
// if there's no init, we assume we're inheriting a non-pjs class, so | ||
// we default to applying the superclass's constructor. | ||
if (!isFunction(proto.init)) { | ||
proto.init = _superclass; | ||
} | ||
// if no init, assume we're inheriting from a non-Pjs class, so | ||
// default to using the superclass constructor. | ||
if (!('init' in proto)) proto.init = _superclass; | ||
@@ -83,0 +69,0 @@ return C; |
@@ -119,3 +119,3 @@ var assert = require('assert') | ||
// but it's the same behavior | ||
IdiomaticSubclass.prototype = Object.create(MyClass.prototype); | ||
IdiomaticSubclass.prototype = new MyClass.Bare; | ||
@@ -246,3 +246,3 @@ it('inherits properly', function() { | ||
it('suppports _super', function() { | ||
it('supports _super', function() { | ||
var mixin1 = function(proto) { | ||
@@ -249,0 +249,0 @@ proto.foo = function() { return 1 }; |
Sorry, the diff of this file is not supported yet
Install scripts
Supply chain riskInstall scripts are run when the package is installed. The majority of malware in npm is hidden in install scripts.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
21009
12
334
0
1