Calling a method of a function constructor from another function in the global scope - Javascript

Mayur Arora

I have the following function constructor

function myClass(funcList) {
this.markDone = function() {
console.log("Done");
}
this.execute = function() {
funcList.forEach(function(func){
func.apply(this);
}); 
}
}

and I have a couple of functions in the global-scope

function func1() {
console.log("func 1");
}

function func2() {
console.log("func 2");
}

var arr = [func1,func2];

I can call these functions from the class's context this way

var ob = new myClass(arr);
ob.execute(); //this does work

How do I invoke markDone from these functions func1 and func2.
If my func1 is

function func1() {
console.log("func 1");
markDone();
} 

and similarly for func 2

This does not work. Shouldn't apply with this take care of the context ?

deitch

Close.

  1. You need to call this.markDone();
  2. this is set differently inside the forEach loop, so you need to set it explicitly or catch it earlier and set it to something else, like that

Try this:

function func1() {
    console.log("func 1");
    this.markDone();
} 

And:

this.execute = function() {
    var that = this;
    funcList.forEach(function(func){
        func.apply(that);
    }); 
}

Here is a fiddle http://jsfiddle.net/8649hu9s/1/

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling global function from constructor

From Dev

Scope chain in Javascript and calling nested function within the global scope

From Dev

Calling a method of a global object from a function

From Dev

Calling JavaScript function from another page

From Dev

Calling a function by name that is not part of the global scope

From Dev

Calling a javascript function from java method in Cordova

From Dev

Calling a global variable in a function in one program from another

From Dev

calling config param from another function (concatenate global error)

From Dev

Calling function from the MainActivity scope

From Dev

AngularJS use a global scope function inside another

From Dev

Python calling function in an function from another function

From Dev

Calling the javascript function in another javascript function

From Dev

Calling a method to function a recyclerview from an onClick() of another recycler adapter class

From Dev

Calling function from another contract using call method

From Dev

Calling an object in constructor in other method/function

From Dev

Calling the this.method() inside function of another method

From Java

Calling a function from another package

From Dev

flask calling a function from another

From Dev

Calling a function from another in ReactJs

From Dev

Calling a function from another Project

From Dev

Calling a function from another process

From Dev

Calling a function from another class?

From Dev

AngularJs accessing scope outside from another function in javascript

From Javascript

Calling a Function defined inside another function in Javascript

From Dev

Calling a function next to another function javascript explanation

From Dev

Issue on Calling a Function In Another Function In JavaScript

From Dev

Calling a function inside another function in JavaScript

From Dev

Javascript: Calling a function inside another function

From Dev

Calling function from setTimeout() method

Related Related

  1. 1

    Calling global function from constructor

  2. 2

    Scope chain in Javascript and calling nested function within the global scope

  3. 3

    Calling a method of a global object from a function

  4. 4

    Calling JavaScript function from another page

  5. 5

    Calling a function by name that is not part of the global scope

  6. 6

    Calling a javascript function from java method in Cordova

  7. 7

    Calling a global variable in a function in one program from another

  8. 8

    calling config param from another function (concatenate global error)

  9. 9

    Calling function from the MainActivity scope

  10. 10

    AngularJS use a global scope function inside another

  11. 11

    Python calling function in an function from another function

  12. 12

    Calling the javascript function in another javascript function

  13. 13

    Calling a method to function a recyclerview from an onClick() of another recycler adapter class

  14. 14

    Calling function from another contract using call method

  15. 15

    Calling an object in constructor in other method/function

  16. 16

    Calling the this.method() inside function of another method

  17. 17

    Calling a function from another package

  18. 18

    flask calling a function from another

  19. 19

    Calling a function from another in ReactJs

  20. 20

    Calling a function from another Project

  21. 21

    Calling a function from another process

  22. 22

    Calling a function from another class?

  23. 23

    AngularJs accessing scope outside from another function in javascript

  24. 24

    Calling a Function defined inside another function in Javascript

  25. 25

    Calling a function next to another function javascript explanation

  26. 26

    Issue on Calling a Function In Another Function In JavaScript

  27. 27

    Calling a function inside another function in JavaScript

  28. 28

    Javascript: Calling a function inside another function

  29. 29

    Calling function from setTimeout() method

HotTag

Archive