8. Operador bind

El operador bind (::) es una nueva característica que está en Proposal, que nos permite vincular el entorno de una función a otra.

Básicamente es azúcar sintático para .bind:

class Test {
  constructor () {
    this.text = 'Mi Texto'
  }

  getText () {
    return this.text
  }
}

let test = new Test()
let func = test.getText

try {
  console.log(func())
} catch(err) {
  console.log('Error!')
}


func = test.getText.bind(test)
console.log(func())

Para vincular el entorno, nombramos la instancia con el operador nuevo, seguido del método.

ES 6
ES 5
class Test {
  constructor () {
    this.text = 'Mi Texto'
  }

  getText () {
    return this.text
  }
}

let test = new Test()
const func = test::test.getText
console.log(func())
'use strict';

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var Test = function () {
  function Test() {
    _classCallCheck(this, Test);

    this.text = 'Mi Texto';
  }

  _createClass(Test, [{
    key: 'getText',
    value: function getText() {
      return this.text;
    }
  }]);

  return Test;
}();

var test = new Test();
var func = test.getText.bind(test);
console.log(func());

Podemos resumir esto, ya que la instancia también es el mismo objeto en el que se encuentra la función:

ES 6
ES 5
class Test {
  constructor () {
    this.text = 'Mi Texto'
  }

  getText () {
    return this.text
  }
}

let test = new Test()
const func = ::test.getText
console.log(func())
'use strict';

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var Test = function () {
  function Test() {
    _classCallCheck(this, Test);

    this.text = 'Mi Texto';
  }

  _createClass(Test, [{
    key: 'getText',
    value: function getText() {
      return this.text;
    }
  }]);

  return Test;
}();

var test = new Test();
var func = test.getText.bind(test);
console.log(func());

results matching ""

    No results matching ""