Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Bug fix

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...
commit ddfd73f470753c3adbd3c424ea78ef4947895f0f 1 parent 2bc2d15
@daankets authored
Showing with 13 additions and 7 deletions.
  1. +3 −0  changelog.md
  2. +9 −6 lib/private.js
  3. +1 −1  package.json
View
3  changelog.md
@@ -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
--------------
View
15 lib/private.js
@@ -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.
*
@@ -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)));
@@ -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;
@@ -113,8 +118,6 @@ Private.enable = function (object, initialValues) {
}
Private(object)[Private.THIS_PRIVATE_VAR_NAME] = object;
-
-
};
// Enable the Private prototype for Private support.
View
2  package.json
@@ -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" : {
Please sign in to comment.
Something went wrong with that request. Please try again.