对于5个“同类”原型函数,我得到了CodeClimate的重复副本,作为导致D评级的重复。如何在JavaScript中结合原型函数样式?
我的知识缺乏如何组合/不重复类似的代码来改善我的“代码风格”。对你的帮助表示感谢。
的原型:
SomeThing.prototype.destination = function(path) {
if (!arguments.length) {
return this.path(this._destination);
}
assert(is.string(path), 'You must pass a destination path string.');
SomeThing.prototype.frontmatter = function(frontmatter) {
if (!arguments.length) {
return this._frontmatter;
}
assert(is.boolean(frontmatter), 'You must pass a boolean.');
SomeThing.prototype.clean = function(clean) {
if (!arguments.length) {
return this._clean;
}
assert(is.boolean(clean), 'You must pass a boolean.');
SomeThing.prototype.concurrency = function(max) {
if (!arguments.length) {
return this._concurrency;
}
assert(is.number(max), 'You must pass a number for concurrency.');
你是什么意思与 “合”?你想避免重复'SomeThing.prototype'?然后使用Object.assign。 – Oriol
我认为重复是arguments.length检查加上相似的断言。 –
我确实相信你们俩都是对的 - @ Travis。我一直在看这[Mozilla文档](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign),但正如我所说,我的知识并没有帮助我有了这个。 – CelticParser