js箭头函数事例
时间: 2023-07-18 22:04:02 浏览: 41
以下是使用箭头函数定义函数的示例:
1. 无参数的箭头函数
```
const sayHello = () => {
console.log("Hello!");
}
sayHello(); // 输出 "Hello!"
```
2. 有一个参数的箭头函数
```
const doubleNum = (num) => {
return num * 2;
}
console.log(doubleNum(5)); // 输出 10
```
3. 有多个参数的箭头函数
```
const addNums = (num1, num2) => {
return num1 + num2;
}
console.log(addNums(2, 3)); // 输出 5
```
4. 箭头函数作为回调函数
```
const nums = [1, 2, 3, 4, 5];
const doubledNums = nums.map((num) => {
return num * 2;
});
console.log(doubledNums); // 输出 [2, 4, 6, 8, 10]
```
相关问题
c++拷贝构造函数事例
以下是一个C++拷贝构造函数的例子:
```c++
#include <iostream>
#include <cstring>
using namespace std;
class StringBad {
private:
char *str;
int len;
static int num;
public:
StringBad(const char *s) {
len = strlen(s);
str = new char[len + 1];
strcpy(str, s);
num++;
cout << "调用了构造函数" << endl;
}
StringBad(const StringBad &st) {
str = new char[strlen(st.str) + 1];
strcpy(str, st.str);
len = st.len;
num++;
cout << "调用了拷贝构造函数" << endl;
}
~StringBad() {
cout << "调用了析构函数" << endl;
delete [] str;
num--;
}
StringBad & operator=(const StringBad &st) {
if (this == &st) {
return *this;
}
delete [] str;
str = new char[strlen(st.str) + 1];
strcpy(str, st.str);
len = st.len;
cout << "调用了赋值运算符" << endl; return *this;
}
friend ostream & operator<<(ostream &os, const StringBad &st) {
os << st.str;
return os;
}
};
int StringBad::num = 0;
int main() {
StringBad s1("hello");
StringBad s2(s1);
StringBad s3 = s1;
StringBad s4("world");
s2 = s4;
cout << s1 << endl;
cout << s2 << endl;
cout << s3 << endl;
cout << s4 << endl;
cout << "当前对象数量:" << StringBad::num << endl;
return 0;
}
```
auto.js数据采集事例
Auto.js是一款基于JavaScript的Android自动化工具,可以用于实现Android设备上的各种自动化操作,包括数据采集。下面是一个使用Auto.js进行数据采集的示例:
```javascript
// 导入Auto.js的相关模块
var packageName = "com.example.app"; // 待采集数据的应用包名
// 启动待采集数据的应用
launch(packageName);
// 等待应用启动完成
sleep(3000);
// 进行数据采集
var data = [];
var listView = className("android.widget.ListView").findOne(); // 假设数据在ListView中展示
var childCount = listView.childCount();
for (var i = 0; i < childCount; i++) {
var item = listView.child(i);
var text = item.text();
data.push(text);
}
// 打印采集到的数据
console.log(data);
// 关闭应用
home(); // 回到桌面
```
这个例子演示了如何使用Auto.js启动指定应用,等待应用启动完成后,通过遍历ListView的子项来采集数据,将采集到的数据存储在一个数组中。最后,打印出采集到的数据并关闭应用。