JCooking
JCooking

Reputation: 21

Browserify/JavaScript, otherjs.myfunction is not a function()

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! :)

Upvotes: 0

Views: 1151

Answers (1)

Bhargav Ponnapalli
Bhargav Ponnapalli

Reputation: 9412

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');

Upvotes: 1

Related Questions