Good day!

There is a code:

OBJ = function(arr){ ON=true; function test(){ console.log(ON); } } OBJ.prototype = { start: function(){ ON = true; OBJ.test;//Доступ к функции test внутри OBJ!!!! }, stop: function(){ ON = false; } } 

This part of OBJ.test; does not work OBJ.test; .
Should run the test function from OBJ , but does not work. At least I think so.

What is wrong?

Update

A function that will work with a picture and create an animation, or rather move by pixels at a certain interval. The test function is responsible for the transition to other pixels (inside it there is a certain interval through which the transition will be made, it is recursive). The start and stop functions provide start and stop of such transitions. Start changes the On parameter to true and calls the test function, which will continue or begin moving through the image, by pixels. Stop will change the ON parameter to false , which will stop the robot of the recursive test function due to if(ON){test();} , which will stop the animation.

    1 answer 1

    Examine scopes to understand the error.

    1. Variables inside functions that are created without the var keyword fall into the global scope and are accessible from any part of the code.
    2. Variables that are created in the inner scope of a function are not accessible from the outside.

    Your work with the ON variable is possible only because it fell into a global area and is accessible from everywhere. But the functions do not fail, and, accordingly, there is no access to it from the outside at all.

    If your goal is to create an object from a function, then you should leave a binding to the object through this. Although, perhaps, I misunderstood your goals.

     OBJ = function(arr){ this.ON = true; this.test = function (){ console.log(this.ON); } } OBJ.prototype = { start: function(){ this.ON = true; this.test(); }, stop: function(){ this.ON = false; } } o = new OBJ(); o.stop(); o.start(); 
    • It seems so, only writes> this.test is not a function If I write this.test(); if i write this.test; it just doesn't work. - M11
    • this.ON why undefined - M11
    • You better tell what you want to do and what behavior you expect. And then just a sheet of code marked "does not work" is very little, for understanding the task in this case. - Alex Krass
    • A function that will work with a picture and create an animation, or rather, go through pixels at a certain interval. The test function is responsible for switching to other pixels (inside it there is a certain interval through which the transition will make, it is recursive). The start and stop functions provide start and stop of such transitions. Start changes the On parameter to true and calls the test function which will continue or begin moving through the image, by pixels. Stop will change the ON parameter to false, which will stop the recursive test function due to if (ON) {test ();} which will stop the animation. - M11
    • @ M11, in fact, I don’t quite understand why you need to use a prototype and such strange scopes if you write a function. I just have the feeling that you are trying to use the possibilities of a language that you do not quite understand and are absolutely not needed here. Therefore, it is difficult for me to advise something specific. - Alex Krass