console.log魔法
时间: 2024-05-17 14:16:18 浏览: 10
console.log是JavaScript中用于在控制台输出信息的函数。它可以将指定的内容打印到浏览器或终端的控制台中,供开发者调试代码或输出结果。例如,你可以使用以下代码在控制台打印一条消息:
```javascript
console.log('Hello, world!');
```
在浏览器的开发者工具中或终端运行上述代码,你会在控制台看到输出的结果:"Hello, world!"。这在开发过程中非常有用,可以帮助你查看变量的值、调试代码等。
相关问题
getPrevNum(index) { console.log(index) // this.list[cloneData['index'] - 1]['ok_qty'] let ls = this.list[index - 1] if (ls['is_reported'] == 1) { let mateArr = this.list.filter((v) => v.group_id == ls['group_id']) // console.log(mateArr) // if (mateArr.length == 1) { // return mateArr[0]['ok_qty'] // } return { ok: mateArr.reduce((pre, x) => { return new Decimal(pre).add(new Decimal(x['ok_qty'])).toNumber() }, 0), ng: mateArr.reduce((pre, x) => { return new Decimal(pre).add(new Decimal(x['ng_qty'])).toNumber() }, 0), total: mateArr.reduce((pre, x) => { return new Decimal(pre).add(new Decimal(x['ok_qty']).add(new Decimal(x['ng_qty']))).toNumber() }, 0), } } let firstItemIndex = this.list.findIndex((v) => ls['group_id'] == v.group_id) console.log(firstItemIndex) if (firstItemIndex - 1 < 0) { return { ok: this.infoData.target_qty, ng: 0, total: this.infoData.target_qty } } else { let mateArr = this.list.filter((v) => v.group_id == this.list[firstItemIndex - 1]['group_id']) // if (mateArr.length == 1) { // return mateArr[0]['ok_qty'] // } return { ok: mateArr.reduce((pre, x) => { return new Decimal(pre).add(new Decimal(x['ok_qty'])).toNumber() }, 0), ng: mateArr.reduce((pre, x) => { return new Decimal(pre).add(new Decimal(x['ng_qty'])).toNumber() }, 0), total: mateArr.reduce((pre, x) => { return new Decimal(pre).add(new Decimal(x['ok_qty']).add(new Decimal(x['ng_qty']))).toNumber() }, 0), } } }, 优化一下
这段代码中可以优化的地方有:
1. 使用箭头函数简化代码:可以将函数内部的所有函数都改为箭头函数,让代码更加简洁易读。
2. 使用解构赋值:可以使用解构赋值来获取函数参数中的属性,让代码更加简洁。
3. 使用三元运算符:可以使用三元运算符来简化条件语句,让代码更加简洁。
4. 避免重复计算:可以避免重复计算,提高代码的效率。
5. 使用常量代替魔法数:可以使用常量来代替一些魔法数,让代码更加易读。
根据上述优化方案,可以改写代码如下:
```
getPrevNum = (index) => {
const OK_QTY = 'ok_qty';
const NG_QTY = 'ng_qty';
const IS_REPORTED = 'is_reported';
const GROUP_ID = 'group_id';
const TARGET_QTY = this.infoData.target_qty;
console.log(index);
const ls = this.list[index - 1];
if (ls[IS_REPORTED] === 1) {
const mateArr = this.list.filter((v) => v[GROUP_ID] === ls[GROUP_ID]);
return {
ok: mateArr.reduce((pre, x) => new Decimal(pre).add(new Decimal(x[OK_QTY])).toNumber(), 0),
ng: mateArr.reduce((pre, x) => new Decimal(pre).add(new Decimal(x[NG_QTY])).toNumber(), 0),
total: mateArr.reduce((pre, x) => new Decimal(pre).add(new Decimal(x[OK_QTY]).add(new Decimal(x[NG_QTY]))).toNumber(), 0),
};
}
const firstItemIndex = this.list.findIndex((v) => ls[GROUP_ID] === v[GROUP_ID]);
console.log(firstItemIndex);
if (firstItemIndex - 1 < 0) {
return { ok: TARGET_QTY, ng: 0, total: TARGET_QTY };
} else {
const mateArr = this.list.filter((v) => v[GROUP_ID] === this.list[firstItemIndex - 1][GROUP_ID]);
return {
ok: mateArr.reduce((pre, x) => new Decimal(pre).add(new Decimal(x[OK_QTY])).toNumber(), 0),
ng: mateArr.reduce((pre, x) => new Decimal(pre).add(new Decimal(x[NG_QTY])).toNumber(), 0),
total: mateArr.reduce((pre, x) => new Decimal(pre).add(new Decimal(x[OK_QTY]).add(new Decimal(x[NG_QTY]))).toNumber(), 0),
};
}
};
```
js class设置变量的魔法方法
在 JavaScript 中,可以使用特定的方法来定义类的变量,这些方法被称为“魔法方法”。以下是一些常用的魔法方法:
1. constructor():构造函数,在创建对象时执行。可以在这个方法中定义类的属性和方法。
2. get/set:用于定义类的属性,get 方法用于获取属性值,set 方法用于设置属性值。例如:
```
class Person {
constructor(name) {
this._name = name;
}
get name() {
return this._name;
}
set name(value) {
this._name = value;
}
}
const person = new Person('Tom');
console.log(person.name); // Tom
person.name = 'Jerry';
console.log(person.name); // Jerry
```
3. static:用于定义类的静态属性和方法,只能通过类名调用。例如:
```
class Math {
static PI = 3.14;
static add(x, y) {
return x + y;
}
}
console.log(Math.PI); // 3.14
console.log(Math.add(1, 2)); // 3
```
4. toString():用于返回类的字符串表示,通常用于调试和日志输出。例如:
```
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
toString() {
return `Person(name=${this.name}, age=${this.age})`;
}
}
const person = new Person('Tom', 18);
console.log(person.toString()); // Person(name=Tom, age=18)
```
除了以上这些魔法方法,JavaScript 还提供了许多其他的方法和语法糖,可以让我们更方便地定义类和操作对象。