scenario :
there is an object, and the method foo ()
is added through the prototype
keyword.
var Graph = new Graph();
//foo
Graph.prototype.foo = function(){
...
}
//fun
Graph.prototype.fun = function(){
...
}
now the logic in the foo ()
method is too complex, so I want to extract some functional code into a new method subfoo ()
.
what should I do at this time?
this is what I did at first:
var Graph = new Graph();
Graph.prototype.foo = function(){
....
function subfoo(){
//fun()
this.fun(); //graphfun(), fun(), thiswindow, graph
}
//subfoo()
subfoo.call(this); //thisgraph, subfoo()
}
however, when I call another Graph.prototype.fun
method in subfoo ()
, when I go to fun ()
, the this
pointer becomes window
, even if I use call
. What you want, of course, is to point to graph.
maybe my idea is wrong. what should I do in scenarios like this that require extraction methods?
there is no such thing as java
which is really object-oriented and easy to understand.