0

I have a for loop like below:

function activate () {
...some code
..here we know what 'this' is
  if( i > 0...) {               
    function setNodeInvisible(ode) {
        for (var i = 0; i < node.children.length; i++) {
            if (node.children[i].isMesh) {
                this._view.setVisibility(node, false, false);
            }
        }
    }
   }
}

but it doesnt know what this is.

outside of this for loop my 'this' is equal to viewport. and i use it fine.

Also here is doesnt know what 'this' is:

function iterateP (parent) {
    this._view.setVisibility(parent, true, false);
}

hoew can i get this is these places?

  • 1
    use a variable such as `var self = this ` outside the loop and use `self` inside your forLoop – Jaya Sep 13 '17 at 01:18
  • That's not possible. The value of `this` doesn't changed inside a loop body. Please provide a [mcve]. – Felix Kling Sep 13 '17 at 01:21
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this Some basic JS knowledge will help you answer your question – Thai Duong Tran Sep 13 '17 at 01:27
  • Can you please post the entire code? 'this' is completely dependent on WHERE you call it. In other words, where do you call iterateP()?? without the entire code we cant tell you what 'this' points to. – HTMLNoob Sep 13 '17 at 01:43
  • please see my edit –  Sep 13 '17 at 02:23
  • It's still not a complete example. You are not showing how `activate` or `setNodeInvisibility` is called. However, the duplicate should help. – Felix Kling Sep 13 '17 at 04:44

2 Answers2

0

the value of this is depends on the caller function context. If you want to specify the value of this, you can use apply() and call() to call the function.

You can read this and this to know more about it.

Harlan
  • 847
  • 5
  • 15
  • Harlan, apply() and call() arent the only ways to specify the value of 'this' It ultimately depends on where he called it and how he will want to call it. – HTMLNoob Sep 13 '17 at 01:45
  • please see my edit –  Sep 13 '17 at 02:31
0

Looks like a design problem, but not enough info to help with that. In the meantime, you can use .call() to use the current this value in the setVisibility call.

this._view.setVisibility.call(this, node, false, false);

The nature of your original call was that this was implicitly set to the _view object because of your call to setVisibility from that object. In other words, the object to the left of the method becomes the value of this.

Using .call lets you manually set the value of this in the function being called to whatever value you want. In this case, we used the current this value by passing it as the first argument.

spanky
  • 2,768
  • 8
  • 9
  • please see my edit –  Sep 13 '17 at 02:31
  • @SarahFar: Sorry, but you really need to take the time to produce a small demo that fully illustrates the issue. All you're showing now is a function inside an `if` statement, which doesn't tell us much, and you're pretty vague in the description. – spanky Sep 13 '17 at 15:11