0

i want to call the isAuthenticated() method within the logout function.

(Im not sure what you call this layout its like a variable object containing functions)

var svc = {
    logout: function () {
        isAuthenticated() // Call isAuthenticated function here
    },
    login: function () {},
    isAuthenticated: function () {}
}

Simply calling isAuthenticated() does not work.

takendarkk
  • 3,347
  • 8
  • 25
  • 37
Kay
  • 17,906
  • 63
  • 162
  • 270

5 Answers5

0

svc is an object, where are logout & isAuthenticated are methods.You can use this to call the function.Here this points to the svc object

var svc = {
  logout: function() {
    this.isAuthenticated() // Call isAuthenticated function here
  },

  login: function() {


  },

  isAuthenticated: function() {
    console.log('isAuthenticated called');
  }
}
svc.logout()
brk
  • 48,835
  • 10
  • 56
  • 78
0

Add this to it.

 var svc = {
                logout: function () {
                    this.isAuthenticated() // Call isAuthenticated function here
                },

                login: function (){


                },

                isAuthenticated: function () {
                    console.log("Test");
                }
}

svc.logout();
user441521
  • 6,942
  • 23
  • 88
  • 160
0

I believe what you are looking for is:

this.isAuthenticated();

You need to use "this" to call other members of the same object.

Malfus
  • 21
  • 1
  • 5
0

Javascript object variables and functions are accessed by prefixing the object name followed by the variable/function name.

In your case, you can call isAuthenticated() like this:

svc.isAuthenticated()
Sanjay Vig
  • 11
  • 3
0

You nee to use the reference of that object svc --> this.isAuthenticated()

var svc = {
  logout: function() {
    this.isAuthenticated()
  },

  login: function() {},

  isAuthenticated: function() {
    console.log("Is Authenticated!?")
  }
}

svc.logout();

Another alternative is creating a function and declaring a variable, this way you will be able to call it directly as follow:

var svc = function() {
  var isAuthenticated = function() {
    console.log("Is Authenticated!?");
  }
  
  this.isAuthenticated = isAuthenticated;
  
  this.logout = function() {
    isAuthenticated()
  };
  
  this.login = function() {};
}

new svc().logout();
new svc().isAuthenticated();
Ele
  • 33,468
  • 7
  • 37
  • 75