How to write JavaScript with factory functions

前端 未结 6 1286
野趣味
野趣味 2021-01-01 00:55

I\'m reading this article about perils of trying to mimic OOP in JavaScript and there\'s the following:

In JavaScript, factory functions are simply co

6条回答
  •  情歌与酒
    2021-01-01 01:10

    The simplest pattern is:

    function RabbitFactory() {
      return {
        speed: 3,
        getSpeed() { return this.speed; }
      };
    }
    
    var rabbit = RabbitFactory();
    

提交回复
热议问题