13

我偶然发现Rect()了 Firefox 和 Chrome(但不是 IE 10)中都存在的函数:

typeof Rect; // "function"
Rect;        // function Rect() { [native code] }

但是这个函数既不能直接访问,也不能作为构造函数访问:

Rect();     // TypeError: Illegal constructor
new Rect(); // TypeError: Illegal constructor

这个函数的目的是什么?

4

2 回答 2

11

Rect文档对象模型 (DOM) 级别 2 样式规范中定义的接口,用于处理rect()DOM 绑定中的 CSS(例如浏览器中的 Javascript DOM 绑定)。

正如您注意到的,您不能自己将其作为构造函数调用,但实现此接口的对象由各种函数返回,例如.getRectValue()

function doSomething(v) {
  if (v instanceof Rect) {
    ...
  }
  else {
    ...
  }
}
doSomething(window.getComputedStyle(elem, null).
  getPropertyCSSValue(styleProp).getRectValue());
于 2013-09-15T17:39:23.657 回答
0

现在你可以创建一个 DOMRect:

var myDOMRect = new DOMRect(x, y, width, height);

请参阅https://developer.mozilla.org/en-US/docs/Web/API/DOMRect/DOMRect

于 2021-01-19T18:26:34.223 回答