0

I'm trying to make something in JS and using browserify to use other JS files in my main code. I know I have node and browserify installed correctly because I can enter "browserify example.js -o bundle.js" in the terminal and run his code just fine.

I finally just added my function (soundoff()) to his system.js code below and tried calling it the same way he called the functions in this file in the second code snippet...

    module.exports = (function (){
  var _final = "",
      DEFAULT_STEP = 15,
      DEFAULT_TURN = Math.PI / 4;



  /* Initializes an L-System using the supplied
   * axiom, rules, and number of iterations
   * params:
   * - axiom: the starting string
   * - rules: the array of rules as string -> string
   *   key/value pairs (i.e. ["key", "value"]
   * - iterations: the number of iterations to carry out
   */
  function lsystem(axiom, rules, iterations) {
    _final = axiom;

    for(var i = 0; i < iterations; i++) {
      _final = apply(_final, rules);
    }

    return _final;
  }

  function soundoff(done) {
    return done;
  }

You see below here I'm calling it the same way he's calling other functions from the ls reference/function/module but I still get "Uncaught TypeError: ls.soundoff is not a function"

window.onload = function() {
  var ls = require('./lsystem.js');

  var wonk = ls.soundoff("wonk!");

  // Set up the algae demo
  var aIter = document.getElementById("algae-iterations"),
      aOut = document.getElementById("algae-output");
  aOut.value = ls.algae(0);
  aIter.onchange = function() {
    aOut.value = ls.algae(aIter.value);
  }

Without calling this function it works perfectly. Those aren't the whole code files but I know everything else if fine since they work, I just can't figure out why I can't have a function I put in like that and call it...

Thanks in advance for the help! :)

1 Answer 1

1

That wont work. Because soundoff is a function within lsystem function, which means soundoff is not accessible as a property.

If you can change your lsystem to export an Object and do this, it will work.

module.exports = {

 soundoff : function(done){
    return done;
}
}

Edit :

Method 1:

file1.js

module.exports = (function func(){

   return {

      soundoff : function(done){
           return done;
      }
    } 
});

file2.js

var func = require('file1');
var obj = func();
obj.soundoff('wonk');

Method 2.

file1.js

module.exports = {

 soundoff : function(done){
    return done;
}
}

file2.js

var obj = require('file1');
obj.soundoff('wonk');

Method: 3

There are other dirty ways where you could, use the this property in conjunction with Function.prototype.call, but I would advise you not to go for it. Do not use it, like, ever. Highly volatile.

file1.js

module.exports = (function func(){

      this.soundoff = function(done){
             return done;

         }

 });

file2.js

var func = require('file1');
func.call(func);
func.soundoff('wonk');
Sign up to request clarification or add additional context in comments.

4 Comments

Okay thanks for the solution, but I don't understand how he's calling other ones, like algae with just ls.algae(param) in that first code snippet which calls this function: function algae(iterations) { return lsystem("A", [["A","AB"],["B","A"]], iterations); }
Can you share the link to the original system.js file?
pastebin.com/xh4aaMJr ^ here's the original sans my function soundoff() thing which uses the same syntax and is in the same code blocks as the other functions that the main file, example.js uses pastebin.com/d8hW2crF and I'm just browserifying it this way >browserify example.js -o bundle.js and everything works perfect except what I added. I'm now armed with your workaround but I want to understand how it's working
O MAN I get it now :) I changed it to var wonk = function() { ls.soundoff("wonk!"); } and it works now... the different syntax in your answer was throwing me off and i get it now... you can have other functions use the module but you can't access it like it's just in your code. Thanks so much :) :) :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.