java中soo_soo_ - SegmentFault 思否

 2023-09-15 阅读 25 评论 0

摘要:抛砖引玉java中的static?实现一个日志功能。组件在挂载前打印 Component will mount组件挂载后打印 Component did mountjava array.sort?不能忍受的写法var AComponent = React.createClass({componentWillMount: function () {console.log('Component will mount&

抛砖引玉

java中的static?实现一个日志功能。组件在挂载前打印 Component will mount

组件挂载后打印 Component did mount

java array.sort?不能忍受的写法var AComponent = React.createClass({

componentWillMount: function () {

console.log('Component will mount');

},

componentDidMount: function () {

console.log('Component did mount');

},

render: function () {

return (

AComponent

)

}

});

var BComponent = React.createClass({

componentWillMount: function () {

console.log('Component will mount');

},

componentDidMount: function () {

console.log('Component did mount');

},

render: function () {

return (

BComponent

)

}

});

看到上面的代码,直接吐血而亡啊,写的是什么几把玩意儿。还好只写了两个组件,要是多个组件,相同的代码就会重复多遍。相信大家看到上面的代码也会发现一个致命的问题:可维护性太差差差!

改进

相信大家都不会写出上面的代码,如果真有人会那样写,请允许我呵呵你。一般都会抽出公共的部分。var Log = {

componentWillMount: function () {

console.log('Component will mount');

},

componentDidMount: function () {

console.log('Component did mount');

}

};

var AComponent = React.createClass({

componentWillMount: function () {

Log.componentWillMount();

},

componentDidMount: function () {

Log.componentDidMount();

},

render: function () {

return (

AComponent

)

}

});

var BComponent = React.createClass({

componentWillMount: function () {

Log.componentWillMount();

},

componentDidMount: function () {

Log.componentDidMount();

},

render: function () {

return (

BComponent

)

}

});

看起来挺完美的,实际上还是有个问题:代码的耦合性太强!像这种日志功能应该同业务分离,不应该直接出现在业务代码中。如果做过Java开发,应该很容易想到一个概念:AOP—面向切面编程。

Mixins

利用React的Mixins,编写的代码就像这样的:var LogMixin = {

componentWillMount: function () {

console.log('Component will mount');

},

componentDidMount: function () {

console.log('Component did mount');

}

};

var AComponent = React.createClass({

mixins: [LogMixin],

render: function () {

return (

AComponent

)

}

});

var BComponent = React.createClass({

mixins: [LogMixin],

render: function () {

return (

BComponent

)

}

});

Mixins有点类似AOP。所谓的mixins就是将组件里的方法抽出来。实际上Mixins里的this是指向组件的,使用了Mixins以后,组件也可以调用Mixins里的方法。

组件调用Mixins方法var Mixin = {

log:function(){

console.log('Mixin log');

}

};

var Component = React.createClass({

mixins: [Mixin],

componentWillMount: function () {

this.log();

},

render: function () {

return (

Component

)

}

});

控制台打印:$ Mixin log

生命周期方法

Mixins里也可以编写组件生命周期的方法,需要注意的是:Mixins里的方法并不会覆盖组件的生命周期方法,会在先于组件生命周期方法执行。var Mixin = {

componentWillMount: function () {

console.log('Mixin Will Mount');

}

};

var Component = React.createClass({

mixins: [Mixin],

componentWillMount: function () {

console.log('Component Will Mount');

},

render: function () {

return (

Component

)

}

});

控制台打印:$ Mixin Will Mount

$ Component Will Mount

使用多个Mixin

组件也可以使用多个Mixinvar AMixin = {

componentWillMount: function () {

console.log('AMixin Will Mount');

}

};

var BMixin = {

componentWillMount: function () {

console.log('BMixin Will Mount');

}

};

var Component = React.createClass({

mixins: [AMixin,BMixin],

componentWillMount: function () {

console.log('Component Will Mount');

},

render: function () {

return (

Component

)

}

});

控制台打印:$ AMixin Will Mount

$ BMixin Will Mount

$ Component Will Mount

数组引入的顺序,决定了Mxins里生命周期方法的执行顺序。

不允许重复

除了生命周期方法可以重复以外,其他的方法都不可以重复,否则会报错

情景1var AMixin = {

log: function () {

console.log('AMixin Log');

}

};

var BMixin = {

log: function () {

console.log('BMixin Log');

}

};

var Component = React.createClass({

mixins: [AMixin,BMixin],

render: function () {

return (

Component

)

}

});

情景2var Mixin = {

log: function () {

console.log('Mixin Log');

}

};

var Component = React.createClass({

mixins: [Mixin],

log:function(){

console.log('Component Log');

},

render: function () {

return (

Component

)

}

});

以上两种情景,都会报错,控制信息如下,所以使用的时候要小心了

soo_查看原文

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/58334.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息