美文网首页
export的各种写法

export的各种写法

作者: 小猪x | 来源:发表于2021-11-04 14:39 被阅读0次

page.js 调用文件

let exportTestUtils = require('@utils/exportTestUtils');
import { firstMethods } from '@utils/exportTestUtils';
----------------------------------------------------------------------------------
console.log('---------使用require方式---------');
let valueRequire = exportTestUtils.firstMethods();
console.log('valueRequire:' + valueRequire);
console.log('---------使用import方式---------');
let valueImport = firstMethods();
console.log('valueImport:' + valueImport);

exportTestUtils.js 工具类文件

写法0
// ----------- 写法0 start -----------
export function firstMethods() {
    console.log('firstMethods-写法0');
    return secondMethods();
}

export function secondMethods() {
    console.log('secondMethods-写法0');
    return '我是写法0';
}
// ----------- 写法0 end -----------
结果
image.png
写法1
// ----------- 写法1 start -----------
export const firstMethods = () => {
    console.log('firstMethods-写法1');
    return secondMethods();
};

export const secondMethods = () => {
    console.log('secondMethods-写法1');
    return '我是写法1';
};
// ----------- 写法1 end -----------
结果
image.png
写法2
// ----------- 写法2 start -----------
const firstMethods = () => {
    console.log('firstMethods-写法2');
    return secondMethods();
};

const secondMethods = () => {
    console.log('secondMethods-写法2');
    return '我是写法2';
};

module.exports = {
    firstMethods: firstMethods,
    secondMethods: secondMethods,
};
// ----------- 写法2 end -----------
结果
image.png
写法3
// ----------- 写法3 start -----------
function firstMethods() {
    console.log('firstMethods-写法3');
    return secondMethods();
}

function secondMethods() {
    console.log('secondMethods-写法3');
    return '我是写法3';
}

module.exports = {
    firstMethods: firstMethods,
    secondMethods: secondMethods,
};
// ----------- 写法3 end -----------
结果
image.png
写法4
// ----------- 写法4 start -----------
module.exports = {
    firstMethods() {
        console.log('firstMethods-写法4');
        return this.secondMethods(); // 这里必须this
    },

    secondMethods() {
        console.log('secondMethods-写法4');
        return '我是写法4';
    }
};
// ----------- 写法4 end -----------
结果
image.png
写法5
// ----------- 写法5 start -----------
exports.firstMethods = function () {
    console.log('firstMethods-写法5');
    return exports.secondMethods(); // 这里必须exports.
};

exports.secondMethods = function () {
    console.log('secondMethods-写法5');
    return '我是写法5';
};
// ----------- 写法5 end -----------
结果
image.png
结论:

1、方法0、1 、2 、3 都是等价的,firstMethods、secondMethods可以认为是全局方法,所以可以直接互相调用
2、方法0、3是es5写法, 方法1、2是es5写法
3、方法4 require时firstMethods方法可以通过this.secondMethods调用, 但import却无法通过firstMethods调用this.secondMethods,只能调用没有依赖的方法
4、写法5用import 方式firstMethods调用secondMethods,可以通过【exports.】调用secondMethods

最后

附上整体测试代码

// // ----------- 写法0 start -----------
// export function firstMethods() {
//     console.log('firstMethods-写法0');
//     return secondMethods();
// }
//
// export function secondMethods() {
//     console.log('secondMethods-写法0');
//     return '我是写法0';
// }
// // ----------- 写法0 end -----------


// // ----------- 写法1 start -----------
// export const firstMethods = () => {
//     console.log('firstMethods-写法1');
//     return secondMethods();
// };
//
// export const secondMethods = () => {
//     console.log('secondMethods-写法1');
//     return '我是写法1';
// };
// // ----------- 写法1 end -----------


// // ----------- 写法2 start -----------
// const firstMethods = () => {
//     console.log('firstMethods-写法2');
//     return secondMethods();
// };
//
// const secondMethods = () => {
//     console.log('secondMethods-写法2');
//     return '我是写法2';
// };
//
// module.exports = {
//     firstMethods: firstMethods,
//     secondMethods: secondMethods,
// };
// // ----------- 写法2 end -----------


// // ----------- 写法3 start -----------
// function firstMethods() {
//     console.log('firstMethods-写法3');
//     return secondMethods();
// }
//
// function secondMethods() {
//     console.log('secondMethods-写法3');
//     return '我是写法3';
// }
//
// module.exports = {
//     firstMethods: firstMethods,
//     secondMethods: secondMethods,
// };
// // ----------- 写法3 end -----------


// // ----------- 写法4 start -----------
// module.exports = {
//     firstMethods() {
//         console.log('firstMethods-写法4');
//         return this.secondMethods(); // 这里必须this
//     },
//
//     secondMethods() {
//         console.log('secondMethods-写法4');
//         return '我是写法4';
//     }
// };
// // ----------- 写法4 end -----------


// // ----------- 写法5 start -----------
// exports.firstMethods = function () {
//     console.log('firstMethods-写法5');
//     return exports.secondMethods(); // 这里必须exports.
// };
//
// exports.secondMethods = function () {
//     console.log('secondMethods-写法5');
//     return '我是写法5';
// };
// // ----------- 写法5 end -----------

相关文章

  • export的各种写法

    page.js 调用文件 exportTestUtils.js 工具类文件 写法0 结果 写法1 结果 写法2 结...

  • 模块化

    1. 导出变量 写法1 注意这种写法只能直接export var不能先声明变量再export错误写法: 只要你直接...

  • 模块化

    有很多种写法,推荐使用写法2 不能先var 一个变量 再export这个变量 这样会报错例如: 写法5export...

  • 模块的export和import

    模块export和import的基本语法: 1. export export有2种写法:可以这样写: 也可以这样写...

  • 各种写法

    根据不同状态进行事件处理 调用时根据状态值取到对应方法 遍历全部成员

  • ng-zorro表单验证

    公共写法 校验并排手机号 18500000000,18500000001 export function i...

  • 模块化开发

    1、export基本使用 export指令用于导出变量,比如下面的代码: 上面的代码还有另外一种写法: 2、导出函...

  • vue 工具类之——获取系统日期,时间,星期

    写法有很多种:例如 const aa = (obj) =>{ return obj;}export {aa}imp...

  • 色值转换hex和rgba互换

    色值转换,总结了一下。ts+类写法。 export class SiderBarStyleColorUtil{ ...

  • WordCount的各种写法

    MapReduce写法(Python) streaming Yarn Hive Spark

网友评论

      本文标题:export的各种写法

      本文链接:https://www.haomeiwen.com/subject/hxxkzltx.html