Skip to main content
Version: 22.9.0

JSHandle 类

¥JSHandle class

表示对 JavaScript 对象的引用。可以使用 Page.evaluateHandle() 创建实例。

¥Represents a reference to a JavaScript object. Instances can be created using Page.evaluateHandle().

句柄可防止引用的 JavaScript 对象被垃圾回收,除非句柄特意为 disposed。当 JSHandles 关联的框架被导航离开或父上下文被破坏时,JSHandles 会被自动处置。

¥Handles prevent the referenced JavaScript object from being garbage-collected unless the handle is purposely disposed. JSHandles are auto-disposed when their associated frame is navigated away or the parent context gets destroyed.

句柄可用作任何评估函数(例如 Page.$eval()Page.evaluate()Page.evaluateHandle())的参数。它们被解析为其引用的对象。

¥Handles can be used as arguments for any evaluation function such as Page.$eval(), Page.evaluate(), and Page.evaluateHandle(). They are resolved to their referenced object.

签名:

¥Signature:

export declare abstract class JSHandle<T = unknown>

备注

¥Remarks

此类的构造函数被标记为内部构造函数。第三方代码不应直接调用构造函数或创建扩展 JSHandle 类的子类。

¥The constructor for this class is marked as internal. Third-party code should not call the constructor directly or create subclasses that extend the JSHandle class.

示例

¥Example

const windowHandle = await page.evaluateHandle(() => window);

属性

¥Properties

属性

修饰符

类型

描述

_

optional

T

用于名义上键入 JSHandle

move

() => 这个

方法

¥Methods

方法

修饰符

描述

asElement()

如果句柄是 ElementHandle 的实例,则为 null 或句柄本身。

dispose()

释放句柄引用的对象以进行垃圾回收。

evaluate(pageFunction, args)

使用当前句柄作为第一个参数来计算给定函数。

evaluateHandle(pageFunction, args)

使用当前句柄作为第一个参数来计算给定函数。

getProperties()

获取表示当前句柄属性的句柄映射。

getProperty(propertyName)

从引用的对象中获取单个属性。

getProperty(propertyName)
jsonValue()

表示引用对象的可序列化部分的普通对象。

评论:

如果对象有 toJSON 函数,则不会调用它。

remoteObject()

提供对支持该句柄的 Protocol.Runtime.RemoteObject 的访问。

toString()

返回 JSHandle 的字符串表示形式。

评论:

在调试过程中很有用。