Home > Blockchain >  JS Spread Operator to normal form?
JS Spread Operator to normal form?

Time:01-25

I have the following code that includes Spread Syntax in JS

const opposite = function (f) { return (function (...args) { return !f(...args) }) };

can it be converted without using Spread Syntax? It seems that if I do use this, some of the js minification tools fail to parse these lines of codes.

CodePudding user response:

You can by using the arguments object in combination with Function.prototype.apply():

The arguments object exist inside every function call and it represents the arguments to the function. Using this we can capture any and all arguments passed to the function.

The apply method on the other hand allow you to call a function by passing an array to represent the arguments.

Combining both we can do this:

const opposite = function (f) {
    return (function () {
        return !f.apply(null, arguments)
    })
};

CodePudding user response:

would be to use the apply method, like this:

const opposite = function (f) { 
    return function () { 
        return !f.apply(this, arguments); 
    }
};

Another way to achieve the same result would be to use the call method and pass the arguments object to the function, like this:

const opposite = function (f) {
    return function () {
        return !f.call(this, ...arguments);
    }
};

Both of these methods allow you to pass an arbitrary number of arguments to the function without using the spread syntax.

CodePudding user response:

 const opposite = function (f) {
  return function () {
    return !f.apply(null, arguments);
  }
};

instead of using the spread operator (...) to pass the arguments to the inner function, the apply() method is used. The apply() method allows you to call a function and pass in an array (or an array-like object) of arguments. In this case, the arguments object is passed in as the second parameter to apply(), which will contain all of the arguments passed to the outer function.

The first parameter of apply is the context of the function, here it is set to null

lets use call and apply.

Using call,

  const opposite = function (f) {
      return function () {
        return !f.call(null, ...arguments);
      }
    };

Using bind,

const opposite = function (f) {
  return function () {
    return !f.bind(null, ...arguments)();
  }
};

The call() method allows you to call a function and pass in the arguments separately, as a list of values. The bind() method is similar but it returns a new function, with the context and the arguments set. The call() or bind() method is called on the inner function, passing null as the context and the arguments object, spread with the spread operator.

The first parameter of call or bind is the context of the function, here it is set to null.

This way, the arguments passed to the outer function will be passed as a list of values to the inner function, achieving the same result as the previous example without using the deprecated apply() method.

  • Related