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`.
This commit is contained in:
Marshall Roch
2014-06-14 20:37:51 -07:00
committed by Paul O’Shannessy
parent 431155d2e2
commit a6cd945d9f
2 changed files with 9 additions and 2 deletions
+3 -1
View File
@@ -549,7 +549,9 @@ function mixStaticSpecIntoComponent(ConvenienceConstructor, statics) {
);
result = createChainedFunction(existingProperty, property);
}
ConvenienceConstructor[name] = result;
ConvenienceConstructor[name] = typeof result === 'function' ?
result.bind(ConvenienceConstructor.type) :
result;
ConvenienceConstructor.type[name] = result;
}
}
@@ -1201,7 +1201,10 @@ describe('ReactCompositeComponent', function() {
abc: 'def',
def: 0,
ghi: null,
jkl: 'mno'
jkl: 'mno',
pqr: function() {
return this;
}
},
render: function() {
@@ -1218,6 +1221,8 @@ describe('ReactCompositeComponent', function() {
expect(Component.ghi).toBe(null);
expect(instance.constructor.jkl).toBe('mno');
expect(Component.jkl).toBe('mno');
expect(instance.constructor.pqr()).toBe(Component.type);
expect(Component.pqr()).toBe(Component.type);
});
it('should support statics in mixins', function() {