有人可以解释这个变量的声明方式吗?(Can someone explain the way this variable is declared?)

编程入门 行业动态 更新时间:2024-10-03 10:47:48
有人可以解释这个变量的声明方式吗?(Can someone explain the way this variable is declared?)

我是javascript的新手,并试图修改我买的网站模板,我有点难以理解变量被声明/使用的某种方式。

该模板具有动态内容功能,因为JQuery扩展声明如下:

window.theme = {}; (function(theme, $) { // function }).apply(this, [window.theme, jQuery]);

然后在一个单独的文件中,它们像这个例子一样被初始化:

(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]);

令我困惑的是:

var $this = $(this), opts;

我通过谷歌搜索了解这种使用逗号运算符的赋值方式意味着:

$(this) = opts; $this = $(this);

在我看来, opts还没有初始化,所以如何/为什么它被用作任务? 这个模式在每个模块上,所以我想在开始进行更改之前理解它。

我做了一些谷歌搜索,发现如果声明var = opt然后它将被提升,但这只是声明而不是初始化。

有人能指出我在这里发生了什么事吗?

I am new to javascript and trying to modify a webiste template I bought and I am having a little difficulty understanding a certain way a variable is being declared/used.

The template has dynamic content funtions as JQuery extentions declared as such:

window.theme = {}; (function(theme, $) { // function }).apply(this, [window.theme, jQuery]);

Then in a seperate file they are initialized like this 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 that is confusing me is:

var $this = $(this), opts;

It is my understanding from googling that this way of assignment with comma operator means:

$(this) = opts; $this = $(this);

To my mind opts hasn't been initialized so how/why is it being used as an assignment? This pattern is on every module so I want to understand it before I start making changes.

I did some googling and found that if var = opt was declared then it would have been hoisted but that is just for declaration not initialization.

Can someone point out to me what is going on here please?

最满意答案

如果我理解正确,那么你需要解释JavaScript init变量。 首先,当JavaScript读取您的代码时 - 它会在函数的开头移动所有变量:

function(){ var a = 1; console.log(a);//1 console.log(b);//undefined var b = 2; }

会像这样寻找JS:

function(){ var a,b;//first of all we declare variables. a = 1; console.log(a);// => 1 console.log(b);// => undefined b = 2; }

但是另一个函数在声明队列中具有更高的优先级,这就是为什么它们将在早期初始化:

function(){ console.log(func)// => function(){return 1;} console.log(a)// => undefined var a = 1; function fun(){return 1;} }

真正发生了什么:

function(){ function fun(){return 1;}//first of all functions var a;//secondary variables console.log(func)// => function(){return 1;} console.log(a)// => undefined a = 1; }

这对你来说是一个小理论,因为我认为你会在最近的将来在代码中遇到类似的东西。

现在让我们找你代码:

var $this = $(this), opts;

以及JavaScript如何阅读它?

var $this, opts;//declare all variables; $this = $(this)//init variable $this

如果您感到困惑,那么它只是您声明的变量之间的分隔符:

var a,b,c,d;

等于:

var a; var b; var c; var d;

祝好运!

If I understand you correctly, then you need to explain how JavaScript init variables. First of all when JavaScript reading you code - it moving all variables at the start of function:

function(){ var a = 1; console.log(a);//1 console.log(b);//undefined var b = 2; }

will be look for JS like this:

function(){ var a,b;//first of all we declare variables. a = 1; console.log(a);// => 1 console.log(b);// => undefined b = 2; }

But another function has more priority in the queue of declaring, thats why they will init earlier:

function(){ console.log(func)// => function(){return 1;} console.log(a)// => undefined var a = 1; function fun(){return 1;} }

and what really happened:

function(){ function fun(){return 1;}//first of all functions var a;//secondary variables console.log(func)// => function(){return 1;} console.log(a)// => undefined a = 1; }

it is a little theory for you, because I think you will meet something like this in code in the nearest future.

And now lets look for you code:

var $this = $(this), opts;

and how JavaScript read it?

var $this, opts;//declare all variables; $this = $(this)//init variable $this

If you was confused by ,, it's just delimiter between variables you declare:

var a,b,c,d;

is equal to:

var a; var b; var c; var d;

Good luck!

更多推荐

本文发布于:2023-07-09 10:17:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1085587.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   声明   方式   declared   variable

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!