ES6导出覆盖功能 [英] ES6 Export Overwriting Function

查看:157
本文介绍了ES6导出覆盖功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何导出此覆盖函数,以便导入模块可以检查函数是否已被调用?

How can I export this overwriting function so that an importing module can check whether the function has been called?

// util.js
export function isPageload() {
  return (!!(isPageload = function() { return false; }));
}

当我用Babel编译它时,我收到这个错误:

When I compile that with Babel, I get this error:

Uncaught TypeError: (0 , _util2.default) is not a function

这是ES5的等价物:

var isPageload = function() {
  return (!!(isPageload = function() { return false; }));
}

console.log(isPageload()); // true
console.log(isPageload()); // false


推荐答案

import isPageload from 'foo';

当你可能想要

import {isPageload} from 'foo';

export function isPageload() {

创建一个命名导出,而不是默认导出,默认导出实时绑定更新目前在Babel中无效

creates a named export, not a default export, and default export live-binding updating currently does not work in Babel.

你对这个问题的看法似乎有点迂回。为什么不做

Your approach to this problem does seem somewhat roundabout however. Why not do

let loaded = true;
export isPageLoaded(){
    let state = loaded;
    loaded = false;
    return loaded;
}

这篇关于ES6导出覆盖功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆