1

我正在尝试为要添加到网站的新功能运行 A/B 测试。过去,我们的团队在页面上显示各种功能之前做过这样的事情:

someUserActionThatEnablesFeature:function(){
    experiments.isUserInControlGroup('new-feature1') && newFeature1Obj.show();
}

someUserActionThatDisablesFeature:function(){
    experiments.isUserInControlGroup('new-feature1') && newFeature1Obj.hide();
}

我发现这非常麻烦,因为我们必须检查是否在我们使用新功能的每个地方都启用了实验。我正在考虑做这样的事情:

function NewFeature1(){
    //constructor
}

NewFeature1.prototype = {
    show:function(){
        //do something
    },
    hide:function(){
        //do something
    },
    //etc
};

//before any objects are declared
if (experiments.isUserInControlGroup('new-feature1')) {
    for(var prop in NewFeature1.prototype) {
        //replace with empty function
        NewFeature1.prototype[prop] = function(){};
    }
}

这里的想法是,我在实例化之前用空存根替换 NewFeature1 类的方法,从而确保如果用户不在控制组中,我对该对象的任何调用都不会做任何事情。

这在某些浏览器中是否危险?在最初的测试中,我无法判断我是否覆盖了 Object 的属性。在 webkit 中,它似乎并没有伤害任何东西。我错过了什么吗?我只需要担心 webkit、FF 和 IE8+。谢谢!

4

1 回答 1

1

我认为这是可以接受的,最好只存根由hasOwnProperty类是否没有继承确定的直接属性。

于 2013-12-07T03:28:59.043 回答