为什么函数会声明“myVariable = myVariable;”?(Why would a function state “myVariable = myVariable;”?)

编程入门 行业动态 更新时间:2024-10-09 19:15:39
为什么函数会声明“myVariable = myVariable;”?(Why would a function state “myVariable = myVariable;”?)

在使用我们的开发团队的一些as2代码的过程中,我遇到了一些令人困惑的声明,其中变量被设置为自己。 我有没有想到这种冗余的原因?

我说的字面意思是这样的:

function timeLine(x,w){ x = x; p = ((x) * 100) / w; t = v.totalTime; n = (t * p) / 100; n = n;

我错过了什么? (当我们在它的时候,(x)是什么?我认为它曾经是(x + z)...)

In the course of working with some as2 code from our dev team, I came across a baffling handful of statements where variables were set to themselves. Is there some reason for such a redundancy that I'm not thinking of?

I'm talking literally like this:

function timeLine(x,w){ x = x; p = ((x) * 100) / w; t = v.totalTime; n = (t * p) / 100; n = n;

What am I missing? (While we're at it, what's with (x)? I assume it used to be (x + z)...)

最满意答案

为什么函数会声明myVariable = myVariable; ?

因为编写它的程序员不称职。 你没有遗漏任何东西,无论谁写的代码最初都不知道他们在做什么。

如果那是AS3代码,它们可能是从函数参数设置一个类变量,但最好用显式方法明确表示变量是不同的:

function foo(bar, baz) { this.bar = bar; this.baz = baz; }

此外,AS3具有可能导致副作用的存取器和增变器。 如果是这种情况,程序员不仅会不称职,而且也会很危险。

Why would a function state myVariable = myVariable;?

Because the programmer who wrote it was incompetent. You're not missing anything, whomever wrote that code originally didn't know what they were doing.

If that was AS3 code, it might be possible that they're setting a class variable from the function parameter, but that's best done explicitly using this to show that the variables are different:

function foo(bar, baz) { this.bar = bar; this.baz = baz; }

Additionally AS3 has accessors and mutators that could cause side-effects. If this was the case, the programmer would not just be incompetent, but dangerous as well.

更多推荐

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

发布评论

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

>www.elefans.com

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