在Sweet.js中访问父母变量 [英] access parents variable in Sweet.js

查看:94
本文介绍了在Sweet.js中访问父母变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个新的面向对象的系统,我需要访问宏的父级的变量.我有以下内容:

I'm creating a new object orientation system and I need to access the variables of the parent of the macro. I have the following:

macro module {
    rule { $i:ident { $e ... } } => {
        var $i = {
            $e ...
        }
    }
}

macro fn {
    rule { $i:ident { $e ... } } => {
        $e ...
    }
}

module x {
    fn name { 

    }
}

我希望能够在fn macro中提供module名称(在本例中为x),因为也许我想做类似$parentModule.prototype.myFunc的操作.但是,如果执行$e$e,它将无法正常工作.有可能吗?

I want be able to, in fn macro, have available the module name, in this case, x, because maybe I want to do something like $parentModule.prototype.myFunc. But if I do $e$e it doesn't works properly. Is it possible?

推荐答案

有几种方法可以做到这一点,但我认为最简单的方法是使用名为

There's a few ways to do this but I think the simplest thing to do is use named patterns:

macro module {
    rule { 
        $i:ident {
            $mbody:(fn $name:ident { $body ...}) ...
        }
    } => {
        $i.prototype.$mbody$name ...
    }
}

module x {
    fn name { 

    }
}

这篇关于在Sweet.js中访问父母变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆