Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. JavaScript
    5. Funktion deklarierte Datei JavaScript

    NEWS

    • Neuer Blog: Fotos und Eindrücke aus Solingen

    • ioBroker@Smart Living Forum Solingen, 14.06. - Agenda added

    • ioBroker goes Matter ... Matter Adapter in Stable

    Funktion deklarierte Datei JavaScript

    This topic has been deleted. Only users with topic management privileges can see it.
    • J
      jacusi last edited by

      file 13-add.js

      function add(a, b) {
        return parseInt(a) + parseInt(b);
      }
      

      die main.js-Datei

      const add = require('./13-add').add;
      console.log(add(3, 5));
      

      Ich versuche, die add-Methode in 13-add.js von main.js aufzurufen, aber wenn ich main.js ausführe, gibt es einen Fehler $./13-main.js zurück

      console.log(add(3, 5));
                  ^
      
      TypeError: add is not a function
          at Object.<anonymous> 
      

      Am Anfang der Datei 13-add.js habe ich add() eingefügt:

      add();
      function add(a, b) {
        return parseInt(a) + parseInt(b);
      }
      

      Der Fehler bleibt jedoch derselbe.

      paul53 1 Reply Last reply Reply Quote 0
      • paul53
        paul53 @jacusi last edited by paul53

        @jacusi
        Mit require() lädt man Module, die als solche deklariert sind.

        'use strict';
        module.exports = function (a, b) {
           return a + b;
        };
        

        Aufruf:

        const add = require('./13-add'); // Pfad muss passen!
        log(add(3,5));
        

        Damit eigene Module mit gesichert werden, habe ich sie unter "iobroker-data.modules" gespeichert.

        const add = require('../../../iobroker-data/modules/13-add'); 
        log(add(3,5));
        
        1 Reply Last reply Reply Quote 0
        • First post
          Last post

        Support us

        ioBroker
        Community Adapters
        Donate

        418
        Online

        31.8k
        Users

        80.0k
        Topics

        1.3m
        Posts

        2
        2
        146
        Loading More Posts
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes
        Reply
        • Reply as topic
        Log in to reply
        Community
        Impressum | Datenschutz-Bestimmungen | Nutzungsbedingungen
        The ioBroker Community 2014-2023
        logo