How to write JavaScript using factory functions

I read this article about the dangers of trying to simulate OOP in JavaScript and there is the following:

In JavaScript, factory functions are just constructor functions minus the requirement new, the global danger of pollution and uncomfortable restrictions (including this annoying initial uppercase letter of the convention).

JavaScript does not need constructor functions, because any function can return a new object. With the dynamic expansion of an object, object literals and Object.create(), we have everything we need - with none of the confusion. And it thisbehaves the same as in any other function. Hooray!

Is it possible to assume that with this approach we should replace this code:

function Rabbit() {
    this.speed = 3;
}

Rabbit.prototype = {
    this.getSpeed = function() {
        return this.speed;
    }
}

var rabbit = new Rabbit();

Wherein:

function RabbitFactory() {
    var rabbit = {
        speed: 3
    };

    Object.setPrototypeOf(rabbit, {
        getSpeed: function() {
            return this.speed;
        }
    })

    return rabbit;
}

var rabbit = RabbitFactory();
+5
6

, . Object.setPrototypeOf, Object.create ( ). , , , , . factory

const protoRabbit = {
    getSpeed: function() {
        return this.speed;
    }
};
function createRabbit() {
    var rabbit = Object.create(protoRabbit);
    rabbit.speed = 3;
    return rabbit;
}

var rabbit = createRabbit();
+1

, 3 JS:

3 ( )

// class
class Rabbit {
  constructor() {
    this.speed = 3; 
    // it would be so nice to have just 'static const speed = 3;' instead of
    // using constructor for that
  }
  getSpeed() {
    return this.speed;
  }
}
let rabbit1 = new Rabbit();

// constructor
function ConstructorRabbit(){ }
ConstructorRabbit.prototype.speed = 3;
ConstructorRabbit.prototype.getSpeed = function() {
  return this.speed;
};
let rabbit2 = new ConstructorRabbit();

// factory
const rabbitProto = {
  speed: 3,
  getSpeed() {
    return this.speed;
  }
};
function factoryRabbit () {
  return Object.create(rabbitProto);
}
let rabbit3 = factoryRabbit();

, , , , , . , " ", , . , , JS ES6, , , .

( ) : ", , , ". , , .

+6

, this. factory :

function createRabbit() {
    var speed = 3;
    return {
        getSpeed: function() {
            return speed;
        }
    }
}

var rabbit = createRabbit();
console.log(rabbit.getSpeed());

: http://radar.oreilly.com/2014/03/javascript-without-the-this.html

+3

:

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

var rabbit = RabbitFactory();
+1

. .

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

setPrototypeOf/getPrototypeOf __proto__ ES6, Object.create() - ES5. setPrototypeOf/getPrototypeOf __proto__ , Object.create()

.

0

, .

:

const RabbitFactory = () => {
  const speed = 3;
  const GetSpeed = () => speed;
  return { GetSpeed }
}
const rabbit = RabbitFactory();
rabbit.GetSpeed() // -> 3
rabbit.speed // -> undefined!

Factory, Constructor, :

  • Object
  • ,
    ( , . , GetSpeed)
0

Source: https://habr.com/ru/post/1654909/


All Articles