Skip to main content
Version: 24.38.0

JSHandle 类

🌐 JSHandle class

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

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

句柄可以防止被引用的 JavaScript 对象被垃圾回收,除非该句柄被有意地释放。当其关联的框架被导航或父上下文被销毁时,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

属性

🌐 Property

修饰符

🌐 Modifiers

类型

🌐 Type

描述

🌐 Description

_

optional

T

用于名义上对 JSHandle 进行类型定义。

🌐 Used for nominally typing JSHandle.

move

() => this

方法

🌐 Methods

方法

🌐 Method

修饰符

🌐 Modifiers

描述

🌐 Description

asElement()

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

🌐 Either null or the handle itself if the handle is an instance of ElementHandle.

dispose()

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

🌐 Releases the object referenced by the handle for garbage collection.

evaluate(pageFunction, args)

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

🌐 Evaluates the given function with the current handle as its first argument.

evaluateHandle(pageFunction, args)

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

🌐 Evaluates the given function with the current handle as its first argument.

getProperties()

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

🌐 Gets a map of handles representing the properties of the current handle.

getProperty(propertyName)

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

🌐 Fetches a single property from the referenced object.

getProperty(propertyName)
jsonValue()

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

🌐 A vanilla object representing the serializable portions of the referenced object.

附注

如果对象有一个 toJSON 函数,它不会被调用。

🌐 If the object has a toJSON function, it will not be called.

remoteObject()

提供对支撑此句柄的 Protocol.Runtime.RemoteObject 的访问。

🌐 Provides access to the Protocol.Runtime.RemoteObject backing this handle.

toString()

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

🌐 Returns a string representation of the JSHandle.

附注

在调试过程中很有用。

🌐 Useful during debugging.