1

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?

asked Jan 30, 2016 at 18:10
2
  • What line is erroring out? Commented Jan 30, 2016 at 18:11
  • If you're going to elide semicolons like some of us do, you should consider adopting a set of practices where you never start a line with a binary or unary postfix operator without including a leading ;. Also, I'd personally avoid using the return value of an assignment as a logical condition like you're doing, as well as logical operators for flow control. Let your minifier handle that. Commented Jan 30, 2016 at 18:29

1 Answer 1

5

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)
answered Jan 30, 2016 at 18:13
Sign up to request clarification or add additional context in comments.

3 Comments

Indeed, I'd rewrite that properly.
@wintercounter: eslint can help you catch those ASI errors.
I have but didn't throw any warning in PhpStorm.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.