javascript - Use module in Node js and reassign values -


i have javascript module simplified 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 how 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 same output twice?

potentially have not understood modules , scopes yet, hint on doc welcome

the "problem" wrong usage of this keyword in function:

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

returning this in context means "return module itself". means assignment (pose.eye.left = ...) (in context of pose.js file):

exports.eye.left = ... 

exports.eye function, in result assigning new member function eye (this possible, because javascript's functions objects).


a proper assignment (without modifications in pose.js file) this:

pose.pose.eye.left = ... 

Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -