简体   繁体   English

工厂函数和模块模式有什么区别? (更多下文)

[英]What is the difference between factory functions and module patterns? (more below)

So I'm trying to wrap my head around the concept of constructors, factory functions, IIFE, closures, module patterns and I find it really confusing when should I use which one.所以我试图围绕构造函数、工厂函数、IIFE、闭包、模块模式的概念来思考,我发现什么时候应该使用哪一个真的很混乱。 What caught my eye is that factory functions and module patterns are really similar, take this for example:引起我注意的是工厂函数和模块模式非常相似,例如:

//module pattern
const calculator = (function(){
  const add = (a,b) => (a+b);
  const sub = (a,b) => (a-b);
  const mul = (a,b) => (a*b);
  const div = (a,b) => (a/b);

  return {add, sub, mul, div}
})();

console.log(calculator.add(3,6));


// same with factory functions

 function calc (){
  const add = (a,b) => a+b;
  const sub = (a,b) => a-b;
  return {add, sub}
}

const calculator2 = calc()
console.log(calculator2.add(3,6))

Can anyone can tell me what is the practical difference?谁能告诉我实际的区别是什么? Any help appreciated!任何帮助表示赞赏!

If you are familiar with constructors, then you already know the concept of inheritance and prototypes, or giving our objects access to the methods and properties of another object.如果您熟悉构造函数,那么您已经知道 inheritance 和原型的概念,或者让我们的对象访问另一个 object 的方法和属性。 There are a few easy ways to accomplish this while using factories.在使用工厂时,有一些简单的方法可以实现这一点。 Check this one out:看看这个:

const Person = (name) => {
const sayName = () => console.log(`my name is ${name}`)
return {sayName}
}

const Nerd = (name) => {
// simply create a person and pull out the sayName function with destructuring assignment syntax!

const {sayName} = Person(name)
const doSomethingNerdy = () => console.log('nerd stuff')
return {sayName, doSomethingNerdy}
}

const jeff = Nerd('jeff')
jeff.sayName() //my name is jeff
jeff.doSomethingNerdy() // nerd stuff

This pattern is great because it allows you to pick and choose which functions you want to include in your new object.这种模式非常棒,因为它允许您选择要包含在新 object 中的功能。

Modules are essentially a syntax for importing and exporting code between different JavaScript files.模块本质上是在不同 JavaScript 文件之间导入和导出代码的语法。 Modules are actually very similar to factory functions.模块实际上与工厂函数非常相似。 The main difference is how they're created.主要区别在于它们的创建方式。 for example:例如:

const calculator = (() => {
const add = (a, b) => a + b;
const sub = (a, b) => a - b;
const mul = (a, b) => a * b;
const div = (a, b) => a / b;
return {
add,
sub,
mul,
div,
}
})();
calculator.add(3,5) // 8
calculator.sub(6,2) // 4
calculator.mul(14,5534) // 77476

The concepts are exactly the same as the factory function.概念与出厂 function 完全相同。 However, instead of creating a factory that we can use over and over again to create multiple objects, the module pattern wraps the factory in an IIFE (Immediately Invoked Function Expression).然而,模块模式不是创建一个我们可以反复使用来创建多个对象的工厂,而是将工厂包装在一个 IIFE 中(立即调用 Function 表达式)。

The concepts are exactly the same as the factory function.概念与出厂 function 完全相同。 However, instead of creating a factory that we can use over and over again to create multiple objects, the module pattern wraps the factory in an IIFE (Immediately Invoked Function Expression).然而,模块模式不是创建一个我们可以反复使用来创建多个对象的工厂,而是将工厂包装在一个 IIFE 中(立即调用 Function 表达式)。

factory: use over and over again to create multiple object工厂:反复使用,创建多个object

module: Immediately Invoked Function模块:立即调用 Function

Reference 参考


Module very useful whenever we want to hide certain parts of an object and only expose an interface to the user of the module.当我们想要隐藏 object 的某些部分并且只向模块的用户公开接口时,模块非常有用。 ( source ) 来源

Example例子

 // we used an immediately invoked function expression // to create a private variable, counter var counterIncrementer = (function() { var counter = 0; return function() { return ++counter; }; })(); // prints out 1 console.log(counterIncrementer()); // prints out 2 console.log(counterIncrementer()); // prints out 3 console.log(counterIncrementer());

As you can see, by using the IIFE, we have tied the counter variable to a function which was invoked and closed but can still be accessed by the child function that increments it.正如您所看到的,通过使用 IIFE,我们将计数器变量绑定到 function 被调用和关闭,但仍然可以被子 function 访问,并增加它。 Since we cannot access the counter variable from outside of the function expression, we made it private through scoping manipulation.由于我们无法从 function 表达式外部访问计数器变量,因此我们通过范围操作将其设为私有。


Factory With a factory function, you can create as many user objects as you want.工厂使用工厂 function,您可以创建任意数量的用户对象。 If you're building a chat app, for instance, you can have a user object representing the current user, and also a lot of other user objects representing all the other users who are currently signed in and chatting, so you can display their names and avatars, too.例如,如果您正在构建一个聊天应用程序,您可以有一个用户 object 代表当前用户,还有许多其他用户对象代表当前登录和聊天的所有其他用户,因此您可以显示他们的姓名还有头像。 ( source ) 来源

Example:例子:

 const createUser = ({ userName, avatar }) => ({ userName, avatar, setUserName(userName) { this.userName = userName; return this; } }); console.log(createUser({ userName: 'echo', avatar: 'echo.png' }));

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

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