@samitouri / QOS-React-2 / commits / a6cd945d9f

Fix 'this' in static methods

binds static methods on the descriptor to the component's actual constructor, so that `foo.constructor.bar()` and `Foo.bar()` run with the same `this`.

Marshall Roch committed Jun 14, 2014 at 20:15 UTC a6cd945d9fb37915933c2cd7c87477305d429aed
2 files changed +9 -2
src/core/ReactCompositeComponent.js
+3 -1
@@ -549,7 +549,9 @@ function mixStaticSpecIntoComponent(ConvenienceConstructor, statics) {
549 );
550 result = createChainedFunction(existingProperty, property);
551 }
552 - ConvenienceConstructor[name] = result;
552 + ConvenienceConstructor[name] = typeof result === 'function' ?
553 + result.bind(ConvenienceConstructor.type) :
554 + result;
555 ConvenienceConstructor.type[name] = result;
556 }
557 }
src/core/__tests__/ReactCompositeComponent-test.js
+6 -1
@@ -1201,7 +1201,10 @@ describe('ReactCompositeComponent', function() {
1201 abc: 'def',
1202 def: 0,
1203 ghi: null,
1204 - jkl: 'mno'
1204 + jkl: 'mno',
1205 + pqr: function() {
1206 + return this;
1207 + }
1208 },
1209
1210 render: function() {
@@ -1218,6 +1221,8 @@ describe('ReactCompositeComponent', function() {
1221 expect(Component.ghi).toBe(null);
1222 expect(instance.constructor.jkl).toBe('mno');
1223 expect(Component.jkl).toBe('mno');
1224 + expect(instance.constructor.pqr()).toBe(Component.type);
1225 + expect(Component.pqr()).toBe(Component.type);
1226 });
1227
1228 it('should support statics in mixins', function() {