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

[Proposal] Support ECMAScript6 Class Standard #31

Open
experdot opened this issue Dec 17, 2019 · 6 comments
Open

[Proposal] Support ECMAScript6 Class Standard #31

experdot opened this issue Dec 17, 2019 · 6 comments
Labels
enhancement New feature or request syntax Suggestions to improve the syntax

Comments

@experdot
Copy link

experdot commented Dec 17, 2019

Definition

吾有一經。名之曰「南山」。是經曰。⋯⋯是謂「南山」之經也。
class Foo {......}

Field

吾有一經。名之曰「生徒」。是經曰。
    其有一言,其名為「名」。
    其有数廿三,其名為「齡」。
是謂「生徒」之經也。
class Foo {
    name;
    age = 23;
}

Constructor & Method

吾有一經。名之曰「鳥」。是經曰。
    其有一言,其名為「名」。
    其有首術。欲行是術。必先得一言。曰「名」。乃行是術曰。
        充其「名」以「名」。
    是謂其首術也。
    其有一術。名之曰「飛」。是謂「飛」之術也。
是謂「鳥」之經也。

有「雞」焉,其状如「鳥」,其「名」曰「「鳳凰」」也。
class Foo {
    name;
    constructor(name) {
        this.name = name;
    }
    fly() {}
}

var foo = new Foo("鳳凰");

Extend

吾有一經。名之曰「鳥」。是經曰。
    其有一言,其名為「名」。
    其有首術。欲行是術。必先得一言。曰「名」。乃行是術曰。
        充其「名」以「名」。
    是謂其首術也。
    其有一術。名之曰「飛」。是謂「飛」之術也。
是謂「鳥」之經也。

吾有一經。名之曰「斥鴳」。状似「鳥」焉。是經曰。
    其有一術。名之曰「飛」。
        吾有一言。曰「「我騰躍而上,不過數仞而下,翺翔蓬蒿之間,恥亦飛之至也」」。書之。
    是謂「飛」之術也。
是謂「斥鴳」之經也。

有「雞」焉,其状如「斥鴳」,其「名」曰「「學鸠」」也。
夫「雞」之「飛」者。施之。
class Foo {
    name;
    constructor(name) {
        this.name = name;
    }
    fly() {}
}

class Bar extends Foo {
    fly() {
         console.log("我騰躍而上,不過數仞而下,翺翔蓬蒿之間,恥亦飛之至也");
    }
}

var bar = new Bar("學鸠");
bar.fly();
@LingDong-
Copy link
Member

Great proposal! I like the 山海經 style.

There also has been a proposal to add object literals. I'm putting the link here as a reference, to make sure the wording/syntax is consistent when they're implemented:

https://github.com/LingDong-/wenyan-lang/issues/20

Thanks!

@LingDong- LingDong- added the enhancement New feature or request label Dec 17, 2019
@duskmoon314
Copy link

It looks great. But I wonder where does 有「雞」焉 come from?

Considering 北冥有鱼,其名为鲲, 有兽焉,其状如羊 and有鸟焉,其状如鸡, I think something like below is more natural:

有「<class A>」焉,其状如「<class B>」,其「<prop>」曰「「<str | int | ...>」」也。
class A
{
    prop1;
}

class B extends A
{
    prop2;
}

What about 有「鳥」焉,其状如「斥鴳」,其「名」曰「「學鸠」」也。 ?


p.s. there are some sentences like 有鸟焉,其状如鸡而三首、六目、六足、三翼.

@LingDong-
Copy link
Member

Update: Object literals are now supported: see https://github.com/LingDong-/wenyan-lang/issues/20#issuecomment-567734481
Classes can be simulated to certain extent with factories.
Real classes are still on the plan, stay tuned. Thanks.

@TongChia
Copy link

TongChia commented May 14, 2020

何不简化为

吾有一「斥鴳」。 名之曰「學鸠」。

我以为 吾有一言 中的就是类型,

吾有一經。名之曰「用户」。是經曰。
    其有一言,其名為「username」。
    其有一术,其名為「持久化」。
        // ...
是謂「用户」之經也。
吾有一「用户」名之曰「甲」其「username」曰「tongchia」
夫「用户」之「持久化」者。施之。

是不是更加顺畅

@ghost
Copy link

ghost commented May 14, 2020 via email

@TongChia
Copy link

TongChia commented May 14, 2020

js 是 弱 type, 但我想要的功能是 class -- OOP 操作
js 本身是支持的(不限于 es6 / ts) 如:

function  Foo () {
   this.count = 0;
}
Foo.protype.increment = function () {
   this.count += 1;
}

如果 wy 支持, 在和 js 互操作时会方便很多 如一些原生API的调用:

new Writable({
  write(chunk, encoding, callback) {}
})
// 或
new Promise(() => {});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request syntax Suggestions to improve the syntax
Projects
None yet
Development

No branches or pull requests

4 participants