class-autobind-decorator
Advanced tools
Comparing version 1.0.2 to 2.0.0
@@ -7,3 +7,3 @@ 'use strict'; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; }; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
@@ -48,3 +48,3 @@ exports.default = autoBindMethods; | ||
function autoBindMethodsDecorator(target) { | ||
var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
@@ -56,4 +56,4 @@ if (typeof target !== 'function') { | ||
var prototype = target.prototype; | ||
var _options$methodsToIgn = options.methodsToIgnore; | ||
var methodsToIgnore = _options$methodsToIgn === undefined ? [] : _options$methodsToIgn; | ||
var _options$methodsToIgn = options.methodsToIgnore, | ||
methodsToIgnore = _options$methodsToIgn === undefined ? [] : _options$methodsToIgn; | ||
@@ -83,2 +83,10 @@ var ownProps = typeof Object.getOwnPropertySymbols === 'function' ? Object.getOwnPropertyNames(prototype).concat(Object.getOwnPropertySymbols(prototype)) : Object.getOwnPropertyNames(prototype); | ||
if (!boundMethod) { | ||
if (!(this instanceof target)) { | ||
// We don't want to bind to something that isn't an instance of the constructor in the rare | ||
// case where the property is read by some means other than an instance *before* it has been | ||
// bound (e.g., if something checks whether the method exists via the prototype, as in | ||
// `someConstructor.prototype.someProp`), so we just return the unbound method in that case. | ||
return value; | ||
} | ||
boundMethod = value.bind(this); | ||
@@ -85,0 +93,0 @@ } |
{ | ||
"name": "class-autobind-decorator", | ||
"version": "1.0.2", | ||
"version": "2.0.0", | ||
"description": "A small framework-agnostic utility for auto-binding \"class\" methods to instances (with customization options) using either \"legacy\" decorator syntax or plain old ES5 (without needing ES2015+ polyfills).", | ||
@@ -5,0 +5,0 @@ "main": "build/index.js", |
@@ -69,2 +69,10 @@ /** | ||
if (!boundMethod) { | ||
if (!(this instanceof target)) { | ||
// We don't want to bind to something that isn't an instance of the constructor in the rare | ||
// case where the property is read by some means other than an instance *before* it has been | ||
// bound (e.g., if something checks whether the method exists via the prototype, as in | ||
// `someConstructor.prototype.someProp`), so we just return the unbound method in that case. | ||
return value; | ||
} | ||
boundMethod = value.bind(this); | ||
@@ -71,0 +79,0 @@ } |
@@ -129,2 +129,14 @@ import 'babel-polyfill'; | ||
}); | ||
describe('when a method is accessed by something other than the instance', function () { | ||
it('should not bind the method, but should only bind when access *is* by the instance', function () { | ||
const [ , MySecondClass, ] = getClasses(); | ||
const myInstance = new MySecondClass(); | ||
myInstance.__proto__.testMethodOne; // similarly, should not bind | ||
const { testMethodOne } = myInstance; | ||
expect(testMethodOne(myInstance)).to.equal(true); // should be bound to the instance now, not the prototype | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19641
8
268