The problem of method extraction and invocation in javascript

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.

all the time.

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.


var me=this
function subfoo () {
.
me.foo ()
}
try


func () ? Where did you get func () ? Can you complete your code, including where you use this , and make up which object you expect this this to point to? I guess you may have used the arrow function, which does not have the this keyword.

I tried the following code (the only prototype of node,node on the phone is _ _ proto__ . It is not convenient to run on the browser)

function Graph() {}

var Graph = new Graph();

Graph.__proto__.foo = function() {
  console.log('old foo', this);
  return this;
}

Graph.__proto__.fun = function() {
  console.log('old fun', this);
  return this;
}

Graph.__proto__.foo = function() {
  console.log('new foo', this);
  function subfoo() {
    console.log('subfoo', this);
    this.fun();
    return this;
  }
  subfoo.call(this);
  return this;
}

Graph.foo();

the result is

new foo Graph {}
subfoo Graph {}
old fun Graph {}
Graph {}

is very normal. I don't know if you have missed any information that is not provided. Of course, it may also be the difference between the browser and node. Besides, what browser did you test on? I can test it on the browser at noon or in the afternoon.


The key reason for

is that I use array in Graph.prototype.fun () . The code of foreach ()
results in:
Graph.prototype.foo calls subfoo.call (this) (no problem, this points to as expected), but Graph.prototype.fun () is called in subfoo () , after fun () is executed. In this way, the caller is no longer a graph instance , then this naturally becomes a global window .
so the key reason is that callers have changed .
if changes the loop mode to a count loop, then OK .
modified code, this points to the correct:

for (var i = 0; i < edges.length; iPP) {
    var edge = edges[i];
    if (edge.data != null && edge.data.type != null) {
        console.log("this.edgeTypeSet");
        console.log(this.edgeTypeSet);
        if (edge.data.type in this.edgeTypeSet) {
            console.log("this.edgeTypeSet:");
            console.log(this.edgeTypeSet);
            this.addEdge(edge);
        }
    } else { //type, , 
        this.addEdge(edge);
    }
}
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b2bfe5-4d17a.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b2bfe5-4d17a.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?