Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

function.length is lost #76

Closed
bertho-zero opened this issue Apr 7, 2021 · 4 comments · Fixed by #79
Closed

function.length is lost #76

bertho-zero opened this issue Apr 7, 2021 · 4 comments · Fixed by #79

Comments

@bertho-zero
Copy link
Collaborator

function.length is lost, and I see an error calling hooks without hooks argument.

const { hooks } = require('@feathersjs/hooks');

const emptyHook = async (context, next) => next();

// Hooks can be used with a function like this:
const sayHello = async name => {
  return `Hello ${name}!`;
};

const sayHello2 = hooks(async name => {
  return `Hello ${name}!`;
});

const sayHello3 = hooks(async name => {
  return `Hello ${name}!`;
}, []);

const sayHello4 = hooks(async name => {
  return `Hello ${name}!`;
}, [emptyHook]);

(async () => {
  console.log(sayHello.length); // 1
  console.log(sayHello2.length); // 3, should be 1
  console.log(sayHello3.length); // 0, should be 1
  console.log(sayHello4.length); // 0, should be 1
  
  console.log(await sayHello('Bertho'));
  // console.log(await sayHello2('Bertho'));
  // Throw an error: TypeError: manager.parent is not a function
  //  at Object.setManager
  console.log(await sayHello3('Bertho'));
  console.log(await sayHello4('Bertho'));
})();
@bertho-zero
Copy link
Collaborator Author

The same may be happening with function.name, I haven't checked.

@daffl
Copy link
Member

daffl commented Apr 7, 2021

Hm, so according to MDN the property is not writeable but configurable. I tested this and it looked like it worked (but not sure if that applies to all engines):

function blabla (a, b, c) {}
Object.defineProperty(blabla, 'length', { value: 5 })
console.log(blabla.length);

@bertho-zero
Copy link
Collaborator Author

bertho-zero commented Apr 7, 2021

image

The configurable seems newer than the function.length, and still not supported on IE.

This redefinition of length and name must be in a try catch in my opinion.

@bertho-zero
Copy link
Collaborator Author

image

As expected, it's broken in IE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants