React使用ES5扩展类 [英] React extends class using ES5
本文介绍了React使用ES5扩展类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是ES6语法,这对我工作OK:
It's ES6 syntax, that work OK for me:
import {Component} from 'react';
class A extends Component {}
class B extends A {
// I can redeclare some methods here
}
如何用ES5实现呢?我的意思是:
And how implement this with ES5? I mean:
var React = require('react');
var A = React.createClass({});
var B = ???
可能有人提供一些例子吗?
Could someone provide some example, please?
推荐答案
我的代码
React.extend=function(parentClass,nprototype){
var childClass=function(){};
childClass.prototype=new parentClass({});
for(var i in nprototype){
childClass.prototype[i]=nprototype[i];
}
return childClass;
}
var Parent=React.createClass({
getContent:function(){
},
render:function(){
return (
<div>{this.getContent()}</div>
);
}
});
var Child=React.extend(Parent,{
getContent:function(){
return "this is child Content";
}
});
这篇关于React使用ES5扩展类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文