Javascript: Defining a map of functions -
$("#searchtype").on('change', function () { var selectionaction = { all: loadall(), competitions: loadall("competitions"), clubs: loadall("clubs"), teams: loadall("teams") }; var selection = $("#searchtype").find('option:selected').val(); selectionaction[selection] });
see above code. thought when selection equals 1 of properties in object, corresponding function called.
e.g. when selection equals competitions
invoke loadall("competitions")
function.
instead finding when enters onchange
function invokes functions.
what doing wrong here?
use anonymous functions create call. storing result of function phone call undefined
var selectionaction = { all: function(){loadall()}, competitions: function(){loadall("competitions")}, clubs: function(){loadall("clubs")}, teams: function(){loadall("teams")} }; var selection = $("#searchtype").find('option:selected').val(); selectionaction[selection]();// create sure phone call anonymous function
or, if prefer brevity,
$("#searchtype").on('change', function () { loadall($("#searchtype").find('option:selected').val().replace("all","").tolowercase()) });
javascript
No comments:
Post a Comment