Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
257 views
in Technique[技术] by (71.8m points)

javascript - ES6链接访问以前的方法(ES6 Chaining Access Previous Method)

I have a problem with ES6 Class Chaining.(我对ES6类链接有问题。)

I have a class as in the example.(我有一个如示例中的类。) I want to access baz() method callback.(我想访问baz()方法回调。) I want to use that callback inside WorkHard class.(我想在WorkHard类中使用该回调。) How can I do that?(我怎样才能做到这一点?) class WorkHard { constructor() { this.value_1 = [] this.value_2 = [] } foo(args) { this.value_1 = args; return this; } baz(cb) { this.value_1 = null; this.value_2 = null; return this; } zoo(args) { this.value_2 = args; return this; } } const WorkHard = new WorkHard(); WorkHard.foo('TestValue_1').baz(() => { console.log('This is baz() method content.'); }).zoo('TestValue_2')   ask by Muhammed Ya?ar Bardaklar translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You pass callback at first argument baz, so you can call it like this:(您在第一个参数baz处传递了回调,因此可以这样调用它:)

class WorkHard { constructor() { this.value_1 = [] this.value_2 = [] } foo(args) { this.value_1 = args; return this; } baz(cb) { this.value_1 = null; this.value_2 = null; cb(); return this; } zoo(args) { this.value_2 = args; return this; } } const workHard = new WorkHard(); workHard .foo('TestValue_1') .baz(() => { console.log('This is baz() method content.'); }) .zoo('TestValue_2');

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...