0

为什么在 AS3 的所有内容中都存在 valueOf() 函数?当这不是多余的时候,我想不出一个例子。在获得价值方面,xx.valueOf()我来说完全一样(除了一个可能需要更多的 CPU 周期)。此外,即使它们在设置某些东西方面可能不一样,x.valueOf() = y(如果甚至合法)也完全没有意义。

我相信这是因为我没有看到的原因。它是什么?我确实尝试了谷歌搜索一分钟。谢谢!

4

4 回答 4

2

正如你所说,它完全是多余的。

valueOf 方法被简单地包含在内,以便 ActionScript 3 符合 ECMA 语言规范(显然,要成为 ECMA 语言还有其他要求 - 我相信 toString 是另一个例子)。

于 2013-08-02T14:45:24.543 回答
1

Returns the primitive value of the specified object. If this object does not have a primitive value, the object itself is returned.

Source: Adobe AS3 Reference http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Object.html#valueOf()

Edit:

A primitive value can be a Number, int, bool, etc... They are just the value. An object can have properties, methods, etc.

Biggest difference, in my opinion though:

primitive2 = primitive1;

In this example, primitive 2 contains a copy of the data in primitive 1.

obj2 = obj1;

In this one, however, ob2 points to the same object as obj1. Modify either obj1 or obj2 and they both reflect the change, since they are references.

In short, valueOf is used when you want to see the primitive representation of an object (if one exists) rather than the object itself.

于 2013-08-02T14:38:04.403 回答
1

这是一个明显的例子

Value比。ValueOf

Value= Thu Jan 2 13:46:51 GMT-0800 2014(value是日期格式)

ValueOf= 1388699211000valueOf在原始时代)

于 2013-12-31T21:51:49.227 回答
1

valueOf不是没用的。它允许为需要原始类型Object的表达式提供值。它在AS3JavaScript中都可用。

如果有人编写了一个接受 int 的函数,你可以将它传递给你的对象(更准确地说,它传递你的对象valueOf()函数的结果)。

有用性受到以下因素的影响:1)没有传递 Object 的事实,因此它只是最外层范围内的 Object,以及 2)它是只读操作的事实,不能进行赋值。

这是我脑海中的几个具体例子:

示例 1:Counter每次读取时自动递增其值的类:

class Counter
{
  private var _cnt:int = 0;
  public function Counter() { }
  public function valueOf():int
  {
    return _cnt++;
  }
  public function toString():String { return ""+valueOf(); }
}

用法:

var c:* = new Counter();
trace(c);     // 0
trace(c);     // 1
trace(2*c+c); // 2*2+3 = 7
trace(c);     // 4

笔记:

  • 我添加了toString()传递,因为函数String优先toStringvalueOf.
  • 您必须将 c 键入为 * 而不是Counter,否则您将收到关于 Counter to Number 的隐式强制的编译器错误。

示例 2: A(只读)指针类型

假设您有一个整数数组,并且您希望对数组中的元素有一个引用(也称为指针)。ECMA 脚本没有指针,但您可以模拟一个valueOf()

class ArrayIntPointer
{
  private var arr:Array;
  private var idx:int;
  public function ArrayIntPointer(arr:Array,
                                  idx:int)
  {
    this.arr = arr;
    this.idx = idx;
  }

  public function valueOf():int
  {
    return arr[idx];
  }
  public function toString():String { return ""+valueOf(); }
}

用法:

  var arr:Array = [1, 2, 3, 4, 5];
  var int_ptr:* = new ArrayIntPointer(arr, 2);

  // int_ptr is a pointer to the third item in the array and
  // can be used in place of an int thanks to valueOf()
  trace(int_ptr); // 3
  var val:int = 2*int_ptr+1;
  trace(val); // 7

  // but it's still an object with references, so I
  // can change the underlying Array, nand now my
  // object's primitive (aka, non-Object types) value
  // is 50, and it still can be used in place of an int.
  arr[2] = 50;
  trace(int_ptr); // 50

  // you can assign int_ptr, but sadly, this doesn't
  // affect the array.

这很漂亮。如果您可以分配指针并影响数组,那将非常巧妙,但不幸的是,这是不可能的,因为它会分配int_ptr变量。这就是为什么我称它为只读指针。

于 2016-06-29T18:29:21.637 回答