0

as a way to finally learn JS I've decided to create a crossword creating tool

piece of my code:

var Crossword = function(content) {
    Object.defineProperties(this, {
        el: {
            value: this.createPlaceholder(content.insertBefore)
        },
        words: {
            value: this.createWords(content.words)
        }
    });
};
Object.defineProperties(Crossword.prototype, {
    createPlaceholder: {
        value: function(afterId) {
            var after = document.getElementById(afterId),
                parent = after.parentNode,
                placeholder = document.createElement("DIV");
            placeholder.classList.add("crossword");
            parent.insertBefore(placeholder, after);
            return placeholder;
        }
    },
    createWords: {
        value: function(data) {
            //create words append them to this.el
        }
    }
)};

is there any way for me to get the this.el value in createWords method without using individual defineProperty functions in the constructor?

i know, the whole idea might not be best, but I'm just wondering if there is a possibility...

  • What's wrong with just getting `this.el`? Or do you think assigning `this.el` in the constructor is wrong? – Bergi Jun 29 '14 at 15:49
  • Why do you use `Object.defineProperties` at all and not simple property assignments? – Bergi Jun 29 '14 at 15:49
  • @Bergi It will make these immutable, which could make sense for `el`. – plalx Jun 29 '14 at 15:55
  • i want to use "this.el", but in "createWords" it returns "undefined", if i pass "this.el" in the constructor as a parameter it behaves the same I'm using defineProperties so the properties later will be unwritable... not sure if I'm going to need that later, but that's just a "practice" example... – kaczmen Jun 29 '14 at 15:58
  • Ah, now I see what your problem is. Kind of related: [Self-references in object literal declarations](http://stackoverflow.com/questions/4616202/self-references-in-object-literal-declarations). I think using two separate `defineProperty` calls would be the best (and easiest) solution. – Bergi Jun 29 '14 at 16:13

0 Answers0