1

I'm trying to run an A/B test for a new feature I'm adding to a website. In the past, our team has done something like this before showing various features on the page:

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

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

I have found this to be pretty kludgey, since we have to check if the experiment is enabled in every place we use the new feature. What I was thinking about doing instead something like this:

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(){};
    }
}

The idea here is that I'm replacing the NewFeature1 class's methods with empty stubs before I ever instantiate it, thereby ensuring that none of my calls on the object will ever do anything if the user isn't in the control group.

Is this dangerous in some browsers? I couldn't tell in my initial tests if I was overwriting Object's properties. In webkit, it didn't seem like it was hurting anything. Am I missing something? I only have to worry about webkit, FF, and IE8+. Thanks!

Daniel
  • 1,789
  • 17
  • 15
  • I don't necessarily think this is dangerous but I'm sure there is a better way. Why does it need stubs? are the functions going to be called even if the user is not in the control group? If they're not in the group I wouldn't bother initializing the object at all unless it's necessary. – jm0 Dec 07 '13 at 02:59
  • Regardless of whether the user is in the control group, the functions get called. That's why I need them to turn into stubs that do nothing if the user is in the control group. – Daniel Dec 07 '13 at 06:04

1 Answers1

1

I think it's acceptable, maybe better to stub only direct properties determined by hasOwnProperty if the class has no inherits.

hulufei
  • 737
  • 1
  • 6
  • 17
  • Yeah, my colleague mentioned that function, but I originally thought I had to instantiate my object to use it. Apparently, it works if you do NewFeature1.prototype.hasOwnProperty('toString'), which equates to false. Object.prototype.hasOwnProperty('toString') equates to true. Will do more testing next week. Thanks. – Daniel Dec 07 '13 at 03:45