Skip to content

Commit

Permalink
Bug fix
Browse files Browse the repository at this point in the history
Additional verification for accidental re-use of the privates instance of the prototype. This one will check for the _this instance on the privates, and compare it with the object upon the definition of a private method.
  • Loading branch information
daankets committed Nov 3, 2012
1 parent 2bc2d15 commit ddfd73f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
Private.JS change log
=====================

1.0.5 -> 1.0.6
--------------
- **2012-11-03 - Bug fix:** prevents the accidental invalid re-use of the privates instance of the Private prototype by subclasses.
1.0.4 -> 1.0.5
--------------

Expand Down
15 changes: 9 additions & 6 deletions lib/private.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Private.hasAccess = function (method) {
/**
* Method used for adding a new PRIVATE method to the prototype of the privates, that will inject the 'privates' as the
* first argument, and will make sure the method is called with respect to 'this' of the current class.
* In fact, the created method becomes a memeber of the 'privates' of the prototype (or instance).
* In fact, the created method becomes a member of the 'privates' of the prototype (or instance).
*
* The function can later be invoked by a function with access to the privates.
*
Expand All @@ -74,6 +74,11 @@ Private.hasAccess = function (method) {
Private.privateMethod = function (name, method) {
"use strict";

if (Private(this)[Private.THIS_PRIVATE_VAR_NAME] !== this){
// This is still the original object, associated with the Private prototype. We need to create a new instance.
Private.enable(this);
}

// Swap the this and privates arguments!
Private(this)[name] = function () {
return method.apply(this[Private.THIS_PRIVATE_VAR_NAME], [this].concat(Array.prototype.slice.call(arguments)));
Expand All @@ -91,13 +96,13 @@ Private.enable = function (object, initialValues) {
"use strict";

if (object) {
if (!object[Private.PRIVATES_VAR_NAME]) {
if (!Private(object)) {
// Create a new object for the base prototype.
object[Private.PRIVATES_VAR_NAME] = {};
} else {
} else if (Private(object)[Private.THIS_PRIVATE_VAR_NAME] !== object){ // This is still the inherited object!
// Use the inherited privates as the prototype for the new privates.
// This prevents sharing of a single privates instance between prototypes and objects.
object[Private.PRIVATES_VAR_NAME] = Object.create(object[Private.PRIVATES_VAR_NAME]);
object[Private.PRIVATES_VAR_NAME] = Object.create(Private(object));
}
if (!object.privateMethod) {
object.privateMethod = Private.privateMethod;
Expand All @@ -113,8 +118,6 @@ Private.enable = function (object, initialValues) {
}

Private(object)[Private.THIS_PRIVATE_VAR_NAME] = object;


};

// Enable the Private prototype for Private support.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name" : "private-js",
"version" : "1.0.5",
"version" : "1.0.6",
"description" : "Clean 'private' support for JavaScript. This library contains classes and utilities that will allow you to add 'privates' to your JavaScript classes in a clean, spec-compliant way. This library will work both within a browser environment and node.js. It was created in order to make it easier for a distributed team of developers to handle privates in a consistent way. The library is based on JavaScript prototypes in order to avoid repetitive creation of functions.",
"main" : "lib/private.js",
"directories" : {
Expand Down

0 comments on commit ddfd73f

Please sign in to comment.