# class:worker
### class: Worker v0.9.0
Worker 类表示一个 [WebWorker](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API)。在页面对象上 `workercreated` 和 `workerdestroyed` 事件被触发,以标识 worker 的生命周期。
```
page.on('workercreated', worker => console.log('Worker created: ' + worker.url()));page.on('workerdestroyed', worker => console.log('Worker destroyed: ' + worker.url()));console.log('Current workers:');for (const worker of page.workers()) console.log(' ' + worker.url());
```
#### Methods
- [worker.evaluate(pageFunction, ...args)](#?product=Puppeteer&version=v1.11.0&show=api-workerevaluatepagefunction-args)v0.9.0
- [worker.evaluateHandle(pageFunction, ...args)](#?product=Puppeteer&version=v1.11.0&show=api-workerevaluatehandlepagefunction-args)v0.9.0
- [worker.executionContext()](#?product=Puppeteer&version=v1.11.0&show=api-workerexecutioncontext)v0.9.0
- [worker.url()](#?product=Puppeteer&version=v1.11.0&show=api-workerurl)v0.9.0
### Methods
#### worker.evaluate(pageFunction, ...args)v0.9.0
- `pageFunction` <[function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function "Function")|[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")> Function to be evaluated in the worker context
- `...args` <...[Serializable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#Description "Serializable")|[JSHandle](#?product=Puppeteer&version=v1.11.0&show=api-class-jshandle "JSHandle")> Arguments to pass to `pageFunction`
- returns: <[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise "Promise")<[Serializable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#Description "Serializable")>> Promise which resolves to the return value of `pageFunction`
如果传递给 `worker.evaluate` 的函数返回一个 [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise "Promise"),那么 `worker.evaluate` 将等待解析并返回它的值。
如果传递给 `worker.evaluate` 的函数返回一个 非\[序列化\]的值,那么 `worker.evaluate` 解析为 `undefined`。
[(await worker.executionContext()).evaluate(pageFunction, ...args)](#?product=Puppeteer&version=v1.11.0&show=api-executioncontextevaluatepagefunction-args) 的快捷链接。
#### worker.evaluateHandle(pageFunction, ...args)v0.9.0
- `pageFunction` <[function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function "Function")|[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")> Function to be evaluated in the page context
- `...args` <...[Serializable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#Description "Serializable")|[JSHandle](#?product=Puppeteer&version=v1.11.0&show=api-class-jshandle "JSHandle")> Arguments to pass to `pageFunction`
- returns: <[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise "Promise")<[JSHandle](#?product=Puppeteer&version=v1.11.0&show=api-class-jshandle "JSHandle")>> Promise which resolves to the return value of `pageFunction` as in-page object (JSHandle)
`worker.evaluate` 和 `worker.evaluateHandle` 之间的唯一区别是 `worker.evaluateHandle` 返回页内对象(JSHandle)。
如果传递给 `worker.evaluateHandle` 的函数返回一个 [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise "Promise"),那么 `worker.evaluateHandle` 将等待解析并返回它的值。
[(await worker.executionContext()).evaluateHandle(pageFunction, ...args)](#?product=Puppeteer&version=v1.11.0&show=api-executioncontextevaluatehandlepagefunction-args) 的快捷链接。
#### worker.executionContext()v0.9.0
- returns: <[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise "Promise")<[ExecutionContext](#?product=Puppeteer&version=v1.11.0&show=api-class-executioncontext "ExecutionContext")>>
#### worker.url()v0.9.0
- returns: <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")>
![](images/pptr.png)
puppeteer.js中文网|class:worker
puppeteer.js中文文档, puppeteer chrome, puppeteer firefox, puppeteer api 中文文档
puppeteer.js中文网包含了Puppeteer中文文档,最新资讯,应用案例等。Puppeteer 是一个 Node 库,它提供了一个高级 API 来通过 DevTools 协议控制 Chromium 或 Chrome。
- Introduction
- 版本记录
- 概要
- puppeteer-vs-puppeteer-core
- 环境变量
- error-handling
- working-with-chrome-extensions
- class:puppeteer
- class:browserfetcher
- class:browser
- class:page
- class:worker
- class:accessibility
- class:keyboard
- class:mouse
- class:touchscreen
- class:tracing
- class:dialog
- class:consolemessage
- class:frame
- class:executioncontext
- class:jshandle
- class:elementhandle
- class:request
- class:response
- class:securitydetails
- class:target
- class:cdpsession
- class:coverage
- class:timeouterror