Web Analytics Made Easy -
StatCounter node.js and "use strict" and probably not knowing what I'm doing - CodingForum


No announcement yet.

node.js and "use strict" and probably not knowing what I'm doing

  • Filter
  • Time
  • Show
Clear All
new posts

  • node.js and "use strict" and probably not knowing what I'm doing

    I'm working on a javascript project in my spare time. I'm trying to be tidy about it, with a namespace-like thing, and prototype stuff. Perhaps I'm doing it all wrong, so perhaps that's the first question, is there a better way to structure my code. Here's the basic structure:

    In Bore.js:

    Bore = {
      someGlobalData: { foo: 1, bar: 2 },
      aGlobalFunction: function() { console.log('Global Function'); }
    In OtherStuff.js:

    if(typeof window === "undefined") { 
      exports.Bore = require('./Bore.js').Bore;
    Bore.someOtherGlobals = { something: 1, somethingElse: 2 };
    Bore.OtherStuff = function() { this.prop1 = 1; this.prop2 = 2; };
    Bore.OtherStuff.prototype.sixTimesNine = function() { return 42; };
    Is this totally the wrong way to go about it? I'm doing it this way only because I saw someone else doing it this way, not because I'm attached to the idea. Is there a more best-practices way?

    If this is an ok way to do it, then I have to ask how to get node.js and "use strict" to get along. This is intended to run in a browser, but I'm trying to run mocha for unit testing. It seems like "use strict" is the best way to go, but node just won't accept any permutation when I try to require('OtherStuff.js'), which means mocha can't get it to run either.

    Am I going about this completely wrong?

    P.S. I previewed this before posting, and the code is all mangled. You'll have to take my word for it that even if it's not best-practices, it's legal javascript -- the main point is that I'm trying, from node (and mocha is also trying), to require Bore.js from OtherStuff.js, and it's Bore.js and the "use strict" that are causing the problem.

  • #2
    When you have 'use strict' then all variables need to be defined using var, let or const when they are first referenced. Is that what you are missing when you define Bore?
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


    • #3
      I should have explained better what I meant when I said node wouldn't accept it. Part of the time, node would reject the script, but the other part of the time, it would give me an empty object when I did a var r = require('./Bore.js'); -- sorry, I was tired! Anyway, with fresh eyes this morning, I've gotten it to work. There's something of black magic about the way node treats the global 'exports' object. In Bore.js, I add a bunch of stuff to it, so when I required Bore.js from OtherStuff.js, I expected exports to already be set up. I actually had to set it up again from OtherStuff.js. That is, I did the following, and now it all works perfectly, getting along with both node and jshint, even with "use strict".

      var Bore;

      if(typeof window === "undefined") {
      exports.Bore = require('./Bore.js').Bore;
      Bore = exports.Bore;

      I had given up on it, but when I saw that someone had answered me here, I was inspired to give it another try. Thanks.