GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — BUG with reassigning · Issue #3699 · nodejs/node

BUG with reassigning #3699

New issue
Copy link
New issue
Copy link
Closed
Closed
BUG with reassigning#3699
Copy link

Description

@celicoo
celicoo
opened on Nov 6, 2015
Issue body actions

~ DELETED ~

Activity

  1. mscdex commented on Nov 6, 2015

    @mscdex
    mscdex
    on Nov 6, 2015
    Contributor
    More actions

    This isn't a bug. module.exports is only assigned once and it is cached by require(). You cannot magically change function references globally and directly like you are expecting.

    However, one solution to this could be to return an object that holds a reference to the current sum() and reference that property in your main script instead. For example:

    // sum.js
    function sum(a) {
      console.log('test');
      exports.sum = function(b) {
        console.log(a + b);
      };
    }
    
    exports.sum = sum;
    // main.js
    var obj = require('./sum');
    
    obj.sum(10);
    obj.sum(15);
    // outputs:
    // test
    // 25
  2. mscdex
    closed this as completedon Nov 6, 2015
  3. celicoo commented on Nov 6, 2015

    @celicoo
    celicoo
    on Nov 6, 2015
    Author
    More actions

    What if i want call sum one time from two other files? @mscdex

  4. mscdex commented on Nov 6, 2015

    @mscdex
    mscdex
    on Nov 6, 2015
    Contributor
    More actions

    If you need separate instances, then return new instances of sum() instead of the same function/instance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.