function - Is this a legit javascript syntax? -
this question has answer here:
i want ask if following function declared correctly , if syntax possible.
(function(){ $(document).on("click", ".mybutton", function(event) { if ($(this).attr("id") == "buttonauftrag") { $.mobile.changepage("#pagetwo"); } } )();
the function should triggered on button click. googled , never found syntax this. first line (function(){ , closing of function confusing me.
there few things take care of
(function() {//<-- function without name $(document).on("click", ".mybutton", function(event) { if (this.id === "buttonauftrag") {//<-- updated line $.mobile.changepage("#pagetwo"); } });//<-- added line })();//<-- executing block declared
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
sample example, name:
var run = function() { //<-- named `run` console.log('running...'); }; //<-- not invoked console.log('after run() declared ...'); run(); //<-- invoked console.log('run() executed ...');
open console... f12
sample example, without name:
(function() { console.log('runner 1 ...'); }); //<-- not invoked console.log('after runner 1 declared ...'); (function() { console.log('runner 2 ...'); })(); //<-- invoked console.log('after runner 2 declared ...'); (function(x) { console.log('runner 3 ...', 'x: ', x); })(56); //<-- invoked parameters console.log('after runner 3 declared ...');
open console... f12
Comments
Post a Comment