简体   繁体   English

在组合中使用Object.assign()时,可以使用相同的函数名称吗?

[英]Can I use the same function names when using Object.assign() in composition?

Context 上下文

I created a new object using a composition paradigm in Javascript. 我使用Javascript中的合成范例创建了一个新对象。

const canUpdateScore = (state) => ({
    update: (spell) => state.score-- 
})

const mage = (name) => {
  let state = {
    score: 100,
  }
  return Object.assign(state, canUpdateScore(state));
}

scorcher = mage()
scorcher.update();    // call the update method
console.log(scorcher.score)    // 99

Question

Would it be confusing to name the assiging method the same as the external function that is returning that method(example below)?, or is it better to use different names (in context above)? 将辅助方法命名为与返回该方法的外部函数相同(在下面的示例中)会引起混淆吗?还是最好使用其他名称(在上面的上下文中)?

const updateScore = (state) => ({
    updateScore: (spell) => state.score-- 
})

const mage = (name) => {
  let state = {
    score: 100,
  }
  return Object.assign(state, updateScore(state));
}

scorcher = mage()
scorcher.updateScore();    // call the update method
console.log(scorcher.score)    // 99

The convention for using composition is "thing-doer". 使用合成的惯例是“做事者”。 Your updateScore composant is a "scoreUpdater" and it "updateScore"'s. 您的updateScore撰写者是“ scoreUpdater”,而它是“ updateScore”。

Would write out like this: 将这样写出来:

const scoreUpdater = (state) => ({
    updateScore: (spell) => state.score-- 
})

Now, update on it's own a rather lousy name both for clarity reasons and for design ones as well. 现在,出于清楚起见和设计方面的原因,对其本身进行update是很糟糕的。 Imagine you thing of another composant which will update the health: healthUpdater which also implements an update method. 想象一下,您会发现另一个将更新运行状况的组件的情况:healthUpdater也将实现update方法。 One of them will override the other. 其中一个将覆盖另一个。

In general, names of properties in composants should in some way, explicitly refer to the composant itself. 通常,组合物的属性名称应以某种方式明确引用组合物本身。

canUpdateScore is definitely a poor name for an object regardless are it implies a boolean, which is not the case. canUpdateScore绝对是对象的较差名称,无论它暗示的是布尔值,事实并非如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 javascript Object.assign 方法中使用所有函数参数 - How can I use all function arguments inside javascript Object.assign method 使用6to5时如何获取Object.assign()在浏览器中工作? - How can i get Object.assign() to work in the browser when using 6to5? Object.assign不是函数 - Object.assign is not a function 如何导出object.assign并使用它的内容在同一文件的另一个函数中进行验证? - How to export object.assign and use it's content for validation in another function of same file? 我如何使用Object.assign()来按需创建此状态对象 - How can I use Object.assign() to create this state object how I want it 什么时候需要使用Object.assign()方法来复制对象的实例? - When is it necessary to use the Object.assign() method to copy an instance of an object? 如何在属性中使用 class 以及使用 Object.assign 时使用 jsdoc? - How to use jsdoc when class in property, and when using Object.assign? 如何使用Object.assign()函数复制/覆盖对象? - How to copy/overwrite an object using Object.assign() function? 当我为Object.assign()使用基元时,结果显示为空对象 - When I use primitives for Object.assign(), the results are presented as empty objects React警告:当我使用Object.assign()时,函数作为React子元素无效 - React warning: functions are not valid as a React child when i use Object.assign()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM