module.exports.views = {
theme: 'themes/theme1/',
layout: this.theme + 'layout'
};
I want the layout property to depend on the theme property. However, when run, the layout property seems to be undefined. What did I do wrong? Please help...
When you run the code this.theme + 'layout'
, this
does not refers to your views
object but it refers to the global object, which as no theme
property.
This should work:
var view = {};
view.theme = 'themes/theme1/';
view.layout = view.theme + 'layout';
module.exports.views = view;
It's not possible to refer to the object being created via object literal notation from within the notation itself. At the moment, this
refers to the window, or global in node.js's case.
You will need to return the value with a getter function.
module.exports.views = {
theme: 'themes/theme1/',
layout: function() {
return this.theme + 'layout';
}
};
Or you can turn it into a constructor.
module.exports.views = function() {
this.theme = 'themes/theme1/';
this.layout = this.theme + 'layout';
};