1

I have wrote a lot of recursive functions which are worked well, But I have a problem with this one. I have a module containing two matrix. So inside the recursive function I use these matrix using this keyword. First call work well, but then this content is different and I cannot access my matrix!

here is my codes:

index.js:

var TileAttack = require('./lib/problem/AttackedTier/TileAttack').TileAttack;
this.board = [
    [0, 1, 0],
    [1, 0, 0],
    [0, 0, 1]
];
this.attackBoard = [
    [0, 0, 0],
    [0, 0, 0],
    [0, 0, 0]
];
this.tileAttack = TileAttack;
this.tileAttack.init(this.board);
this.tileAttack.checkDown(0, 0, this.tileAttack.isQueenIn(0, 0));

TileAttack.js:

let board;
let attackBoard;
let boardSize;

function init(board) {    
    this.board = board;
    this.attackBoard = [
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]
    ];
    this.boardSize = this.board.length;
}

function checkRight(i, j, wasQueenInPath) {             
    checkAttack(i, j, wasQueenInPath, i, j + 1, checkRight);
}

function checkDown(i, j, wasQueenInPath) {
    checkAttack(i, j, wasQueenInPath, i + 1, j, checkDown);
}

function checkAttack(currentI, currentJ, wasQueenInPath, nextI, nextJ, 
                  currentMove) {
    let queenSeen = false;
    if (currentI >= this.boardSize) return queenSeen;

    if (isQueenIn(currentI, currentJ)) {
        queenSeen = true;
        currentMove(nextI, nextJ, queenSeen);
        return queenSeen;
    }

    if (wasQueenInPath) this.attackBoard[currentI][currentJ]++;

    queenSeen = currentMove(nextI, nextJ, wasQueenInPath);
    if (queenSeen) {
        this.attackBoard[currentI][currentJ]++;
        return queenSeen;
    }
}

function isQueenIn(i, j) {
    return this.board[i][j] == 1;
}

exports.TileAttack = {init, checkDown, checkRight, isQueenIn}

The point which is different here from my previous tries is that I exports an object of functions here. in previous samples I just exports one function.

EDIT

These codes are worked well:

function sort(low, high) {
    if (high <= low) return;

    let pivotIndex = partition(low, high);
    sort(low, pivotIndex - 1);
    sort(pivotIndex + 1, high);
}

function partition(low, high) {
    let pivot = this.arr[low];    
    let pivotIndex = low;
    for (i = low + 1; i <= high; i++){
        if (pivot > this.arr[i]) {
            pivotIndex++;
            exchange(i, pivotIndex);            
        }            
    }
    exchange(low, pivotIndex);    
    return pivotIndex;
}

What's the diffrence between my second codes and first one? Can any one get the problem?

Boolood
  • 77
  • 8
  • 2
    `this` in javascript doesn't depend on the static location of where the function is written but on the dynamic nature of how the function is invoked. [Read more on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this) – nem035 Jun 02 '19 at 11:29
  • 1
    Where are you doing this? Node.js? A browser? If using a browser, which bundler/etc. are you getting `require` from? – T.J. Crowder Jun 02 '19 at 11:31
  • I'm using Nodejs – Boolood Jun 02 '19 at 11:33
  • @nem035 So how should I do this? I tried with class, closure and this codes. all of them behave similar! – Boolood Jun 02 '19 at 11:35
  • 1
    `if (isQueenIn(currentI, currentJ)) {` should be `if (this.isQueenIn(currentI, currentJ)) {` (note the `this.`). (Same for `checkAttack`.) Also, your use of `this` in `index.js` is suspect. `this` in a Node.js module is a ref to the module object. There's no good reason in your example to use `this.board = ...` vs. `const board = ...` (and the same for `attackBoard` and `tileAttack`). Indeed, there's no reason in `index.js` for both `TileAttack` and `tileAttack`. But more importantly, this (no pun) is a **very** suspect structure. Note that all uses of `TileAttack.js` use the **same** object. – T.J. Crowder Jun 02 '19 at 11:35
  • @T.J.Crowder When I use this, I get this error: `TypeError: this.isQueenIn is not a function` – Boolood Jun 02 '19 at 11:43
  • @Boolood - As I said in the comment, you also have to update `checkAttack`. You also have to update various other uses (or non-uses) of `this`, as covered by the linked question's answers. – T.J. Crowder Jun 02 '19 at 11:47
  • **If** you want to switch to using constructor functions, prototypes, and `this` for this, see [here](https://pastebin.com/zR4WpzJ5) and [here](https://pastebin.com/MWQkQJkG). But if you want to use a closure instead, see [here](https://pastebin.com/kD3Hvrk9) and [here](https://pastebin.com/kErs4uje). – T.J. Crowder Jun 02 '19 at 11:48

0 Answers0