I was looking for a way to self-reference an object to access it's property when I discover the ES5 getter
feature (see. MDN).
I'm working with NPM module and can't access the canvas
property.
config.js
import path from "path";
import layout from "./layouts";
const config = {
canvas: { width: 80, height: 80 },
layout: {
get base() {
console.log(this);
console.log(this.canvas);
return layout.base.bottom(this.canvas);
},
get annotation() {
return layout.annotation.top(this.canvas);
}
}
};
export default config;
layouts.js
export default {
annotation: {
top: options => ({
x: options.width / 2,
})
},
base: {
bottom: options => ({
x: options.width / 2,
})
}
};
Error
{ base: [Getter], annotation: [Getter] }
undefined
TypeError: Cannot read property 'width' of undefined
at Object.bottom (/data/projects/hanzi-pinyin-font/src/layouts.js:13:10)
at Object.get base [as base] (/data/projects/hanzi-pinyin-font/src/config.js:17:26)
at generateSvg (/data/projects/hanzi-pinyin-font/index.js:15:40)
at /data/projects/hanzi-pinyin-font/index.js:39:5
at /data/projects/hanzi-pinyin-font/node_modules/jsonfile/index.js:46:5
Question
Why isn't this working?