zoukankan      html  css  js  c++  java
  • javascript导入自定义模块

    问题:

    javascript  如何导入一个自定义的类

    javascript 如何导出一个自定义的类

    javascript 导入自定义的类的语法

    E:project_tsabcmain.js:1
    import {Queue} from './Queue'
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module


     这里,请看这里。以下是标准的 自定义模块的 导出、导入 语法:


    两个文件,分别是Queue.js, Main.js。

    运行 Main.js ,可以正常导出 Queue.js里面定义的类

    请注意: 导入模块、导出模块的语法

    /** Queue.js
     *封装一个队列的结构,提供若干操作队列的接口
     *
     *author:Henry
     *date:20200503
     * 
     */
    function Queue() {
        this.queue = [];
      };
      
      Queue.prototype.enqueue = function(value) {
        this.queue.push(value);
      };
      Queue.prototype.dequeue = function() {
        return this.queue.shift();
      };
      Queue.prototype.peek = function() {
        return this.queue[0];
      };
      Queue.prototype.length = function() {
        return this.queue.length;
      };
      Queue.prototype.print = function() {
        console.log(this.queue.join(' '));
      };
    
    
    module.exports = Queue;
    // Main.js
    
    const Queue = require('./Queue')
    
    
    let que1 = new Queue();
    que1.enqueue(1);
    que1.enqueue(2);
    que1.print();
  • 相关阅读:
    python3.5过滤网址和图片的函数自己亲测可用
    关于接口数据编写的思路整理
    Mysql db
    在Maven中设置Nexus私有服务为中央工厂
    maven 镜像使用
    maven 自建库
    Windows批处理(cmd/bat)常用命令小结
    springmvc and maven
    spring 源码解析
    spring aop 原理
  • 原文地址:https://www.cnblogs.com/music-liang/p/12822106.html
Copyright © 2011-2022 走看看