Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

调用计数器(支持重置) #91

Open
Sunny-117 opened this issue Nov 3, 2022 · 2 comments
Open

调用计数器(支持重置) #91

Sunny-117 opened this issue Nov 3, 2022 · 2 comments

Comments

@Sunny-117
Copy link
Owner

class Count {
}
const instance = new Count()
console.log(instance.add())//1
console.log(instance.add())//2
console.log(instance.add())//3
console.log(instance.reset())//1
console.log(instance.add())//1
@bearki99
Copy link

class Count {
    constructor(value = 0){
        this.value = value;
    }
    add(){
        this.value++;
        return this.value;
    }
    reset(){
        this.value = 1;
        return this.value;
    }
}
const instance = new Count()
console.log(instance.add())//1
console.log(instance.add())//2
console.log(instance.add())//3
console.log(instance.reset())//1
console.log(instance.add())//2

最后一个应该是2吧

@kangkang123269
Copy link

class Count {
    constructor() {
        this.count = 0;
    }

    add() {
        return ++this.count;
    }

    reset() {
        this.count = 0;
        return this.add();
    }
}

const instance = new Count();
console.log(instance.add()); // 输出 1
console.log(instance.add()); // 输出 2
console.log(instance.add()); // 输出 3
console.log(instance.reset()); // 输出 1
console.log(instance.add()); // 输出 2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants