0

I have a javascript module simplified as an eye pose.

var pose = {};
var eye = {};
var left = {};
left.pitchPos = 37;
left.yawPos = 47;

exports.init = function () { 
    eye.left = left;
    pose.eye = eye;
    return this;
};

exports.eye = function (e) {
    if(typeof(e) !== "undefined"){
        pose.eye = e;       
    }
    return pose;
};

exports.pose = pose;

This is how I use it:

var pose = require('./pose').init();
console.log(JSON.stringify(pose));
pose.eye.left = { yawPos: 99, pitchPos: 11 };
console.log(JSON.stringify(pose));

Why do I get the same output twice?

Potentially I have not understood modules and scopes yet, any hint on doc is welcome

1
  • What are you expecting this to refer to? Commented Sep 22, 2016 at 16:13

2 Answers 2

1

The "problem" is with wrong usage of this keyword in this function:

exports.init = function () { 
    eye.left = left;
    pose.eye = eye;
    return this;
};

Returning this in this context means "return the module itself". This means that your assignment (pose.eye.left = ...) does something like this (in context of pose.js file):

exports.eye.left = ...

exports.eye is a Function, so in result you are assigning a new member to the function eye (this is possible, because JavaScript's Functions are Objects).


A proper assignment (without modifications in pose.js file) would look like this:

pose.pose.eye.left = ...
Sign up to request clarification or add additional context in comments.

2 Comments

excellent thanks. any hint where to find a doc on propper creation of javascript modules ?
Your code is a proper module. You just have to be careful with this keyword.
1

There are a couple of things that need to updated. First off when declaring a variable using the var, const or let keywords in the module scope these variable are local only for the module itself. Think of them as "private". So your pose, eye and left variables are visible only inside your module. Also returning this would return the current module, basically all everything linked via the exports property (I think).

What I would suggest is doing something like this:

module.js

function Module() {
    this.pose = {
        eye: {
            left: {
                pitchPos: 37,
                yawPos: 47
            }
        }
    }
}

Module.prototype.setLeftEye = function(pitchPos, yawPos) {
    this.pose.eye.left.pitchPos = pitchPos;
    this.pose.eye.left.yawPos = yawPos;
}

module.exports = Module;

and where you use it:

var Module = require('./mod');

var mod = new Module();

console.log(JSON.stringify(mod.pose));
mod.setLeftEye(99, 11);
console.log(JSON.stringify(mod.pose));

Please note that is pretty much a basic example and you could expand on it. However this code assumes that you need more than one instance of the module across your application.

Comments

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.