javascript - Can someone explain the way this variable is declared? -
i new javascript , trying modify webiste template bought , having little difficulty understanding way variable being declared/used.
the template has dynamic content funtions jquery extentions declared such:
window.theme = {}; (function(theme, $) { // function }).apply(this, [window.theme, jquery]);
then in seperate file initialized example:
(function($) { 'use strict'; if ($.isfunction($.fn['themepluginanimate'])) { $(function() { $('[data-plugin-animate], [data-appear-animation]').each(function() { var $this = $(this), opts; var pluginoptions = $this.data('plugin-options'); if (pluginoptions) opts = pluginoptions; $this.themepluginanimate(opts); }); }); } }).apply(this, [jquery]);
the bit confusing me is:
var $this = $(this), opts;
it understanding googling way of assignment comma operator means:
$(this) = opts; $this = $(this);
to mind opts
hasn't been initialized how/why being used assignment? pattern on every module want understand before start making changes.
i did googling , found if var = opt
declared have been hoisted declaration not initialization.
can point out me going on here please?
if understand correctly, need explain how javascript init variables. first of when javascript reading code - moving variables @ start of function:
function(){ var = 1; console.log(a);//1 console.log(b);//undefined var b = 2; }
will js this:
function(){ var a,b;//first of declare variables. = 1; console.log(a);// => 1 console.log(b);// => undefined b = 2; }
but function has more priority in queue of declaring, thats why init earlier:
function(){ console.log(func)// => function(){return 1;} console.log(a)// => undefined var = 1; function fun(){return 1;} }
and happened:
function(){ function fun(){return 1;}//first of functions var a;//secondary variables console.log(func)// => function(){return 1;} console.log(a)// => undefined = 1; }
it little theory you, because think meet in code in nearest future.
and lets code:
var $this = $(this), opts;
and how javascript read it?
var $this, opts;//declare variables; $this = $(this)//init variable $this
if confused ,
, it's delimiter between variables declare:
var a,b,c,d;
is equal to:
var a; var b; var c; var d;
good luck!
Comments
Post a Comment