Refactor Javascript objects and properties

752 views Asked by At

I have some code I may want to refactor many times. Imagine, something like this:

Namespace.CustomObject = function(name,type){

  this.name = name;
  this.type = type;

};

Namespace.CustomObject.prototype = {

  writeName: function(){
    console.log(this.name);
  },

  changeType: function(type){
    this.type = type;
  }

};

var object1 = new Namespace.CustomObject('object1','spoon');

object1.name = 'new name';
object1.changeType('new type');

So, what I would like is to have a system to generate some sort of data structure where I can know which namespaces/objects I have, and their methods, so I can decide if I want to change "changeType" function name to "setNewType", I can know that it's not just enough with replacing the prototype "changeType" property, but also object1 call to that function, because it is a CustomObject.

So, I have been trying different options.

At first I tried with grasp:

http://graspjs.com/docs/equery/

But I can't find a way to detect the scope of elements, and I think it's not possible.

Then I thought of using esprima to create the AST of my code and maybe get the structure from there that would allow me to identify objects scope, but I can't find a way to do this either.

http://esprima.org/

I guess this must be possible, but maybe I'm trying to wrong way. I know it is also a big task, so of course I'm not asking for someone to solve this, but any hint pointing in the right direction would be very helpful.

Thanks.

0

There are 0 answers