0

I am trying to create a library for a project, its like this:

module.exports = Diary;
function Diary() {

    someFunction = function( message ) {
        console.log(message)
    }

    function D() {
        return new D._Section;
    }

    D.about = {
        version: 1.0
    };

    D.toString = function () {
        return  "Diary "+ D.about.version;
    };

    var Section = function () {
        this.Pages = []
    }

    D._Section = Section;

    //to extend the library for plugins usage
    D.fn = sectionproto = Section.prototype = D.prototype;

    sectionproto.addPage = function (data) {
        this.Pages.push(data)
        conole.log(this.Pages)
    };
    return D;
};

main purpose for this is to use same library for server side and client side operations, so we can have same code base.

this issue is that when i use this in node app

var Diary = require('./diary.js');
var myDiary = new Diary();

console.log(myDiary.addPage('some text on page'))

and run it, it throws an error TypeError: myDiary.addPage is not a function

i am not not sure what to do here to make this work for node js, as our client app is very huge and making changes to it would require some effort if we have to go in some other pattern.

First Question is: 1. is this approach right or we need to look in to something else 2. if this can work on node js app then how with minimum changes to library

2
  • That's really convoluted. What's the reason for all the seemingly-unnecessary layers? Commented May 18, 2017 at 13:12
  • i copied the structure from Raphaeljs as i need some layers for this application and yes i am trying to learn Javascript and Nodejs @JasonSebring Commented May 18, 2017 at 13:26

1 Answer 1

1

The main problem is that you're exporting your overall Diary function, but then using it as though you'd received its return value (the D function) instead.

The way you're exporting it, you'd use it like this:

var Diary = require('./diary.js')();
// Note -------------------------^^
var myDiary = new Diary();

But beware that that means every import will create its own D function and associated things.

Alternately, export the result of calling Diary, but then the Diary function has no purpose.


Other issues are:

  • You're falling prey to The Horror of Implicit Globals* by not declaring someFunction or sectionproto. Be sure to declare your variables.
  • The structure is over-complicated without any obvious reason it needs to be so complicated.

* (disclosure: that's a post on my anemic little blog)

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for answer it helped and code is working with it. sure will cover those global declarations.

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.