Google关闭和生成的获取器/设置器 [英] Google Closure and generated getters/setters

查看:73
本文介绍了Google关闭和生成的获取器/设置器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让KineticJS与Google Closure Compiler一起使用.但是KineticJS生成了它的getters和基于变量名称的设置器.

I'm trying to get KineticJS to work with Google Closure Compiler. KineticJS, however, generated it's getters & setters based on the name of the variables.

类似这样的东西:

// add getter and setter methods
Kinetic.Node.addSetters = function(constructor, arr) {
    for(var n = 0; n < arr.length; n++) {
        var attr = arr[n];
        this._addSetter(constructor, attr);
    }
};
Kinetic.Node.addGetters = function(constructor, arr) {
    for(var n = 0; n < arr.length; n++) {
        var attr = arr[n];
        this._addGetter(constructor, attr);
    }
};
Kinetic.Node.addGettersSetters = function(constructor, arr) {
    this.addSetters(constructor, arr);
    this.addGetters(constructor, arr);
};
Kinetic.Node._addSetter = function(constructor, attr) {
    var that = this;
    var method = 'set' + attr.charAt(0).toUpperCase() + attr.slice(1);
    constructor.prototype[method] = function() {
        var arg;
        if(arguments.length == 1) {
            arg = arguments[0];
        }
        else {
            arg = Array.prototype.slice.call(arguments);
        }
        var obj = {};
        obj[attr] = arg;
        this.setAttrs(obj);
    };
};
Kinetic.Node._addGetter = function(constructor, attr) {
    var that = this;
    var method = 'get' + attr.charAt(0).toUpperCase() + attr.slice(1);
    constructor.prototype[method] = function(arg) {
        return this.attrs[attr];
    };
};
// add getters setters
Kinetic.Node.addGettersSetters(Kinetic.Node, ['x', 'y', 'scale', 'rotation', 'opacity', 'name', 'id', 'offset', 'draggable', 'dragConstraint', 'dragBounds', 'listening']);

因此,使用addGettersSetters方法生成getters&基于变量名称的设置器.

so, with the addGettersSetters method you generate getters & setters based on the names of the variables.

但是Google闭包无法解释这一点,并给出警告(不是错误,只是警告,但仍然...):警告-属性setImage从未在Kinetic.Image上定义

Google closure can't interpret this however, and gives warning (not error, just warnings, but still...): WARNING - Property setImage never defined on Kinetic.Image

我可以为每个吸气剂添加外部元素&二传手.或者,我可以重写所有的getter/setter东西,以使其成为实际的方法(非生成方法).最后一部分似乎是最好的,因为此后Closure可以优化它们并用它做一些魔术(我希望.)尽管这会花费很多工作,所以我想知道,是否有可能运行我的程序,因此它可以生成吸气剂二传手,然后提取他们?或者,如果有人有更好的主意,也欢迎您.

I could add externs to for each getter & setter. Alternatively I could rewrite all the getter/setter stuff, to make it actual methods (non-generated ones) by hand. That last part seems the best, since then Closure can optimize those and do some magic with it (I hope.) Though that would be quite a bit of work, so I was wondering, is it possible to run my program, so it generates the getters & setters, and then extracting them? Or, if someone has a better idea, that's welcome too.

谢谢! -巴勃罗

推荐答案

哪个答案最好取决于您所使用的编译模式以及您的库方式.

Which answer is best depends on which compilation mode you are using and how you are the library.

如果您正在使用ADVANCED模式并使用其他源代码编译该库,则重写可能是最好的方法(我假设这是您当前要执行的操作).但是,如果要单独加载库或将库源代码与ADVANCED编译后的源并置,则外部定义是一个不错的选择(如果库维护人员不支持Closure Compiler ADVANCED编译,这是一个好方法).

If you are using ADVANCED mode and compiling the library with your other sources, rewriting is likely best (I assume this is what you are trying to do currently). However, if you are loading the library separately or concatenating the library source to your ADVANCED compiled source, extern definitions are a good option (this is a good approach if library maintainers don't support Closure Compiler ADVANCED compilation).

人们已经创建了各种各样的工具来帮助自动为库生成外部变量,但是通常最好是有人创建外部变量并将其保存在库中.一些库外部库托管在Closure Compiler源存储库中,并由社区维护,因此始终是一个选择.

There are various tools that people have created to help auto-generate externs for libraries but it usually best if someone creates the externs and maintains them with the library. Some library externs are hosted in the Closure Compiler source repository and maintained by the community so that is always an option.

这篇关于Google关闭和生成的获取器/设置器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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