简体   繁体   中英

Javascript Export a function in a function to a function in another file

I have a problem with how to export a function inside a function to another function in another file. I am using React as my framework.

The code below doesn't work and I have tried several ways to solve it but nothing is working.

In File1 I have several functions, where all of them are supposed to be returned in a View except for the signOut() function. Thus, I want to export my signOut() function and use it in another file. My function in File1 looks like:

function Login() {

     function signIn(){
              }

     function signUp(){
              }
        
     function signOut(){
              }
 
  return signOut, h(LoginView, {
        signIn,
        signUp    
    })

export default Login

In File2 I want to use my SignOut in MenubarView.

import Login from './login.js'

function Menubar({}) {

    return h(MenubarView, { 
        signOut: Login.signOut()                 
    });
  } 

You are not calling your Login function, that returns the object with other functions.

import Login from './login.js'

function Menubar({}) {

    return h(MenubarView, { 
        signOut: Login().signOut()                 
    });
  }

And you nee dto fix the return to return object:

function Login() {

     function signIn(){
              }

     function signUp(){
              }
        
     function signOut(){
              }
 
  return {signOut, somethingElse: h(LoginView, {
        signIn,
        signUp    
    })}

export default Login

To work without Login() call, you would need to add those functions to the Login function object itself.

function signIn(){}

function signUp(){}
        
function signOut(){}

function Login() {
  return h(LoginView, {
        this.signIn,
        this.signUp    
    })}

login.signOut = signOut;
login.signIn = signIn;
login.signUp = signUp;

export default Login

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM