Strange Behavior When Importing Students' Libraries

I and my students have been seeing some strange behavior when testing their classmates’ libraries.

Let’s say Stacy imports Michael’s library, called MathStuff. Stacy tests some functions with her own console.log statements. What she sees in the console is the result of every console.log statement in the MathStuff library. Do students need to delete all the console.log statements before sharing their library?

Also, let’s say Stacy then imports Amanda’s library, called ListFunctions, but hasn’t removed the MathStuff library. Stacy deletes her console.log statements testing MathStuff, and writes new console.log statements testing ListFunctions. When she runs the program, all the console.log tests in MathStuff still show up in Stacy’s console, in addition to all console.log statements in ListFunctions. Even when she runs a completely blank program, she still gets output in the console from the console.log statements in both the libraries.

Is this a bug, or is there some way around this?

The library works by taking every function in the program, and making it a property of an object, the name of the library.

Before

function foo(){

}
function bar(){

}

After

var Library_Name = (function() {
    function foo(){
    
    }
    function bar(){
    
    }
    return {foo:foo,bar:bar}
})()

However, not just functions are carried over.
Before

function foo(){

}
function bar(){

}
console.log("123")

After

var Library_Name = (function() {
    function foo(){
    
    }
    function bar(){
    
    }
    console.log("123")
    return {foo:foo,bar:bar}
})()

Notice that the code above will log “123”, without any of its methods being called. This can be either a good or bad thing, but yes, the console.log’s will have to be either commented or removed.

1 Like