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
wintercounter
7,4686 gold badges36 silver badges52 bronze badges
1 Answer 1
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
elclanrs
94.2k21 gold badges137 silver badges171 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
elclanrs
Indeed, I'd rewrite that properly.
elclanrs
@wintercounter: eslint can help you catch those ASI errors.
wintercounter
I have but didn't throw any warning in PhpStorm.
lang-js
;. 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.