wintercounter
wintercounter

Reputation: 7488

Existing function "is not a function"

Check the console with this JSFiddle which says the following in Chrome:

Uncaught TypeError: handlerHelper(...) is not a function

"use strict"

function parseRoute(route, Handlers){
  let parsed = Handlers
  route.split('->').forEach(function(s){
    parsed.hasOwnProperty(s) && (parsed = parsed[s])
  })
  return parsed
}

function handlerHelper(Handlers, ev){
  let parsed = parseRoute(ev.name, Handlers)
  applyHandlers(parsed, ev)
}

function applyHandlers(obj, ev){
  for (let i in obj) { 
    if (obj.hasOwnProperty(i)){
      console.log(handlerHelper, typeof handlerHelper)
      typeof obj[i] === 'object'
        && handlerHelper(obj[i], ev)

      (i = obj.handlers)
        && i.length
        && i.forEach(function(fn){
        fn.apply(null, ev)
      })
    }
  }
}

handlerHelper({
  $: {
    handlers: [function(){}]
  }
}, {
  name: '$->Test'
})

On the console it clearly says it is function. This is a recursive function and it throws only after in the 3rd iteration. Really weird. Any clues about what's the problem?

Upvotes: 1

Views: 131

Answers (1)

elclanrs
elclanrs

Reputation: 94131

The lack of semicolons is the issue:

    && handlerHelper(obj[i], ev)

  (i = obj.handlers)

This is the same as:

    && handlerHelper(obj[i], ev)(i = obj.handlers)

And handlerHelper does not return a function, thus the error. You need a semicolon there:

    && handlerHelper(obj[i], ev)

  ;(i = obj.handlers)

Upvotes: 5

Related Questions