how can i check if a anonymous function has a property?

Firstly, you are testing e[0] after you have shifted the first value out, therefore on the last iteration, you’ll be checking undefined since for an empty array e[0] === undefined

Secondly, this.a = value does NOT add a property to a function – unless that function is used like new fn() – then the resulting object WILL have a property called a

you could

! function(e) {
  for (; e.length;) {
    const fn = e.shift();
    var o = new fn();
    console.log(o.hasOwnProperty('a'));
  }
}([function() {
    this.a = function() {
      console.log("hello");
    };
  },
  function() {
    //no property here
  }
]);

Alternatively, you could define that first function like

(() => {
  const fn = function() { }; 
  fn.a = function(){ 
    console.log("hello"); 
  }; 
  return fn; 
})()

Which would make your code

! function(e) {
  for (; e.length;) {
    const fn = e.shift();
    //fn(); // you don't actually need to run it
    console.log(fn.hasOwnProperty('a'));
  }
}([(() => {
    const fn = function() {};
    fn.a = function() {
      console.log("hello");
    };
    return fn;
  })(),
  function() {
    //no property here
  }
]);

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top