简体   繁体   English

为什么要在这里使用箭头函数而不是类或对象构造函数?

[英]Why should I use Arrow Functions here instead of a Class or Object Constructors?

I'm working on a small project, mainly to help teach myself JavaScript, and I have a question about a suggestion someone had. 我正在做一个小项目,主要目的是帮助自己教JavaScript,而且我对某人的建议有疑问。

My goal is to have a list of races and rpg classes that when I create a new character object I can assign it a race and an rpg class that it will gain the attributes of. 我的目标是要有一个种族和rpg类的列表,当我创建一个新的角色对象时,我可以为其分配一个种族和一个rpg类,它将获得其属性。

Here's my method and my reasoning behind it. 这是我的方法及其背后的原因。 I'm using a class to define the objects (I'm pretty sure is what I'm doing) that way I can create functions for those classes/objects to do things. 我正在使用一个类来定义对象(我很确定这是我正在做的事情),这样我可以为这些类/对象创建函数来执行操作。 Like on the Player class I can then create functions that manage the Player's inventory. 像在Player类上一样,我然后可以创建管理Player清单的功能。 and when I instantiate a new player I can assign them a race object and an rpg class object. 当我实例化一个新玩家时,我可以为他们分配一个竞赛对象和一个rpg类对象。

class Orc {
  constructor() {
    this.mortal = true
  }
}

class Player {
  constructor(race, name, level){
    var inventory = {};
    this.race = race;
    this.name = name;
    this.level = level;
    this.inventory = inventory;
    this.currentCapacity = 0;
    this.maxCapacity = 50;
  }
}

const orc = new Player(new Orc(), 'Baz', 1 )
console.log(orc.race)

And here's the method I was suggested to use: 建议使用以下方法:

const raceTemplates = {
  human: { mortal: true }, 
  elf: { mortal: true }
}

const playerMaker = template => (race, name, level) => {
  return {
    name,
    level,
    race,
    inventory: {},
    currentCapacity: 0,
    maxCapacity: 50,
    ...template[race]
  }
}  

const player = playerMaker(raceTemplates)('elf', 'sammy', 2)
console.log(player) 

I'm relatively new to JavaScript and I've seen the Arrow Functions before however I'm pretty much entirely self taught so the theory of why some things are better than others is where I fall short on. 我对JavaScript相对较新,并且我以前见过Arrow函数,但是我几乎完全是自学的,因此我不了解为什么某些事物比其他事物更好的理论。 I think I understand what Arrow Functions are but as I've said I'm new to JavaScript and self taught (have PluralSight course offerings through work that I've used) so I'm not too familiar with them. 我想我理解箭头函数是什么,但是正如我所说的,我是JavaScript的新手,并且自学了(通过我使用过的工作获得了PluralSight课程产品),所以我不太熟悉它们。

Any help is appreciated and if this isn't a good question or it's already been answered I apologize. 非常感谢您的帮助,如果这不是一个好问题,或者已经得到答复,我深表歉意。 Thank you! 谢谢!

Of course this is opinionated, but to me a "classic" formulation of: 当然,这是自以为是的,但对我来说,是“经典”的表述:

function playerMaker(template,race,name,level){
...
}

Would be clearer for someone reading it. 对于阅读它的人来说会更清晰。 The arrow syntax is just syntax sugar to not write "function". 箭头语法只是不编写“功能”的语法糖。 But here, creating a nested function that is returned from the master function to be invoked later seems both unintuitive and inefficient to me, in lack of any example where it would be useful as written. 但是在这里,创建一个从主函数返回以供以后调用的嵌套函数对我来说既直观又效率低下,因为缺少任何示例,该函数在编写时会很有用。

Reasons not to use Classes in Javascript: 不使用Java 类的原因:

  1. The concept of “Class” doesn't exist in JavaScript. “类”的概念在JavaScript中不存在。
    • JavaScript classes, introduced in ECMAScript 2015, are primarily syntactical sugar over JavaScript's existing prototype-based inheritance. ECMAScript 2015中引入的JavaScript类主要是对JavaScript现有的基于原型的继承的语法糖。 The class syntax does not introduce a new object-oriented inheritance model to JavaScript. 类语法不会向JavaScript引入新的面向对象的继承模型。

  2. Concept of classes makes things brittle. 类的概念使事情变得脆弱。 Prototypes are better and very flexible (IMO, although I still will use them from time to time). 原型更好,也非常灵活(IMO,尽管我仍会不时使用它们)。
  3. It guides people away from goodness and power of functional programming. 它引导人们远离功能编程的精髓和力量。
    • In JS functions are first-class citizens. 在JS中,功能是一等公民。 Functional programming is all about using functions to their fullest extent. 函数式编程就是最大程度地使用函数。 There is a notion called: “Favor Composition over Inheritance” and here we are going in the opposite direction because “Class” notation favors “Inheritance over Composition”. 有一个概念叫做:“继承时优先考虑组成”,而在这里我们朝着相反的方向发展,因为“类”表示倾向于“继承时优先考虑组成”。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM