【问题标题】:What does the $ in property name mean in ActionScript 3 (or JavaScript)属性名称中的 $ 在 ActionScript 3(或 JavaScript)中是什么意思
【发布时间】:2014-07-01 18:14:09
【问题描述】:

最近看到这段代码,

    public function CursorManager($target:InteractiveObject, $cursor:String) {
        _target=$target;
        _cursor=$cursor;
        _target.addEventListener(MouseEvent.ROLL_OVER, onOver);
        _target.addEventListener(MouseEvent.ROLL_OUT, onOut);
    }

为什么这个人在参数$target 中使用$?它有什么重要意义吗? 我也在 JavaScript 中看到了这一点。

更新:
是否有可能用于表示静态或常量变量?在此示例中,它似乎没有用作静态。

更新 2:
我在 Flex UIComponent 类中发现了一些使用 $width、$height 的代码。我已经添加了一个答案。

【问题讨论】:

  • 为什么要传递这样的静态变量或常量变量? (对我个人来说毫无意义)。只是指出它是一个参数/局部变量。
  • 我同意,在这种情况下它没有任何意义。我已经看到它多次用作静态,但我也看到它用于表示基类中的超级变量。
  • PHP 变量声明需要。在 AS3 中使用它的人可能有一些 PHP 背景。除此之外,它没有任何意义,也不符合任何已知的 as3 编码约定。

标签: java javascript actionscript-3


【解决方案1】:

在这个例子中我会说作者使用 $ 前缀来标识来自外部的变量(函数参数)和前缀 _ 来标识属于这个函数或整个类的变量。

所以答案是不,它没有任何重要意义。这样做只是为了方便。您可能需要查看代码命名约定以了解更多信息。

【讨论】:

  • 我认为变量名只能是字母、数字和下划线,但必须以字母或下划线开头。美元符号似乎可以使用并且没有任何意义。
  • @1.21gigawatts 在 AS3 中,您也可以以下划线 _ 和美元符号 $ 开头。
  • 我要补充一点:使用美元符号是相当少见的。下划线用于类变量(适用于各种 IDE - 它们列在代码提示列表的顶部)。
  • 通过命名约定静态变量通常用大写字母。很好的例子:MouseEvent.ROLL_OUT。 ROLL_OUT 是 MouseEvent 类的 STATIC 变量。在这种情况下,我们需要查看函数外部的内容才能知道 _ 代表什么。 $ - 这里绝对不是静态的。
  • @MaxGram 请不要将静态变量与常量混淆。 ROLL_OUT 是一个常数。从逻辑上讲,常量确实是静态的,但并非每个静态变量都是常量。常量大写。
【解决方案2】:

当我编写 JavaScript 代码时,我对 jQuery-Objects 使用这种表示法,如下所示:

var $target = $(event.target);
$target.hide();

其他人可能会以不同的方式使用它。

【讨论】:

  • 是的,我在 JQuery 中看到了这一点。看来他们已经为自己的邪恶用途接管了单一美元符号。
  • @1.21gigawatts 在 jQuery 中它确实有一个目的 - 您是在提醒自己/其他开发人员该变量包含一个 jQuery 对象,因此您可以在其上调用 jQuery 方法,而不用 $ 包装它(...)(再次)。
【解决方案3】:

正如 Max 所说,“答案是否定的,它没有任何重要意义。这样做只是为了方便。”但我想添加更多。编译器对它没有任何不同的处理(据我所知),但在 Flex 中,它用于表示基本属性,它是最终的,不能被覆盖,这是本机播放器实现的反映。

例如,在属性 $scaleX 中它声明:

/**
 *  This property allows access to the Player's native implementation
 *  of the <code>scaleX</code> property, which can be useful since components
 *  can override <code>scaleX</code> and thereby hide the native implementation.
 *  Note that this "base property" is final and cannot be overridden,
 *  so you can count on it to reflect what is happening at the player level.
 */

$width 属性相同:

/**
 *  @private
 *  This property allows access to the Player's native implementation
 *  of the 'width' property, which can be useful since components
 *  can override 'width' and thereby hide the native implementation.
 *  Note that this "base property" is final and cannot be overridden,
 *  so you can count on it to reflect what is happening at the player level.
 */

mx_internal final function get $width():Number
{
    return super.width; // UIComponent extends FlexSprite which extends Sprite
}

UIComponent 类中所有以 $ 开头的属性都反映了这些属性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-26
    • 1970-01-01
    • 1970-01-01
    • 2017-06-11
    • 1970-01-01
    • 2022-01-24
    • 2015-07-15
    • 2018-07-17
    相关资源
    最近更新 更多