首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Famo.us - this.add和this_add

Famo.us - this.add和this_add
EN

Stack Overflow用户
提问于 2014-06-17 18:12:21
回答 2查看 79关注 0票数 0

在this.add框架中使用this._add和Famo.us有什么区别?我对_add部分(在变量或方法名“模拟”私有变量或方法前面下划线的naimin约定)并不感到困惑,但在代码中没有_add方法。

示例:

代码语言:javascript
复制
 function _createStripViews(){

    this.stripViews = [];
    this.stripModifiers = [];

    var stripData = [
        {title: 'APPAREL', iconUrl: './img/apparel_icon.png', stripColor: '#00aaac', textTitle: './img/apparel_text.png'},
        {title: 'FOOTWEAR', iconUrl: './img/footwear_icon.png', stripColor: '#006a6d', textTitle: './img/footwear_text.png'},
        {title: 'ALL MATERIALS', iconUrl: './img/allMaterials_icon.png', stripColor: '#be326a', textTitle: './img/allMaterials_text.png'},
        {title: 'CHEMISTRY', iconUrl: './img/chemistry_icon.png', stripColor: '#32900e', textTitle: './img/chemistry_text.png'},
        {title: 'ENERGY/GREENHOUSE GAS', iconUrl: './img/energyGreenhouse_icon.png', stripColor: '#cc4300', textTitle: './img/energyGreenhouse_text.png'},
        {title: 'WATER/LAND', iconUrl: './img/waterLand_icon.png', stripColor: '#1a81b6', textTitle: './img/waterLand_text.png'},
        {title: 'PHYSICAL WASTE', iconUrl: './img/physicalWaste_icon.png', stripColor: '#ccb200', textTitle: './img/physicalWaste_text.png'},
        {title: 'RECYCLED', iconUrl: './img/recycled_icon.png', stripColor: '#7d0ea2', textTitle: './img/recycled_text.png'},
        {title: 'ORGANIC', iconUrl: './img/organic_icon.png', stripColor: '#6c00c7', textTitle: './img/organic_text.png'}
    ];

    for(var i = 0; i < stripData.length; i++){

        var stripView = new StripView({
            width: this.options.stripWidth,
            height: this.options.stripHeight,
            title: stripData[i].title,
            color: stripData[i].stripColor,
            iconUrl: stripData[i].iconUrl,
            textTitle: stripData[i].textTitle,
            index: i
        });

        this.stripViews.push(stripView);

        var yOffset = this.options.topOffset + this.options.stripOffset * i;

        var stripModifier = new Modifier({
            transform: Transform.translate(0, yOffset, 0)
        });

        this.stripModifiers.push(stripModifier);
        this._add(stripModifier).add(stripView);

        stripView.pipe(this);
        stripView.on('tap', this.animateStrips.bind(this));

    };
};
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-06-17 18:38:12

其实没有什么区别。如果您查看Famo.us框架中的文件Famo.us,您将看到以下内容。

代码语言:javascript
复制
/**
 * Alias for add
 * @method _add
 */
View.prototype._add = View.prototype.add;

他们是完全一样的东西。我的猜测是,如果要向自定义视图添加' add‘函数,仍然可以使用_add引用视图'add’函数。

希望这能有所帮助!

票数 1
EN

Stack Overflow用户

发布于 2014-06-23 18:28:09

在以面向对象的方式进行编程时,使用_-prefix是JavaScript中的常见模式。它通常意味着实例方法/变量是私有的,或者是受保护的,因为JavaScript并不真正允许您以一种很好的方式这样做。

因此,即使您可以同时使用这两种方法,而且它们目前所做的完全相同,我还是建议您使用.add,因为它可能是应该公开的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24270632

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档