chore: add example for return this

This commit is contained in:
LongYinan 2021-11-25 23:42:40 +08:00
parent c365c15639
commit e6f341f632
No known key found for this signature in database
GPG key ID: C3666B7FC82ADAD7
5 changed files with 12 additions and 0 deletions

View file

@ -97,6 +97,7 @@ Generated by [AVA](https://avajs.dev).
export class ClassWithFactory {␊
name: string␊
static withName(name: string): ClassWithFactory␊
setName(name: string): this␊
}␊
export namespace xxh3 {␊
export const ALIGNMENT: number␊

View file

@ -112,6 +112,10 @@ test('class factory', (t) => {
const duck = ClassWithFactory.withName('Default')
t.is(duck.name, 'Default')
const ret = duck.setName('D')
t.is(ret.name, 'D')
t.is(ret, duck)
duck.name = '周黑鸭'
t.is(duck.name, '周黑鸭')

View file

@ -87,6 +87,7 @@ export class Context {
export class ClassWithFactory {
name: string
static withName(name: string): ClassWithFactory
setName(name: string): this
}
export namespace xxh3 {
export const ALIGNMENT: number

View file

@ -9,4 +9,10 @@ impl ClassWithFactory {
pub fn with_name(name: String) -> Self {
Self { name }
}
#[napi]
pub fn set_name(&mut self, name: String) -> &Self {
self.name = name;
self
}
}