0

I'm starting to figure this out a little better. But I still have a question. Suppose I've got function class that looks like the following:

module.exports = myFunc;

var containerValue = element(by.css('div#container'));

function myFunc() {}

myFunc.prototype = {
    section1: {
        __myBlock: containerValue.$('div#myBlock'),
        myRow: {
            label: function() {
                return __myBlock.$('label');
            }
            input: function() {
                return __myBlock.$('input');
            }
        }
    }
}

First, let me point out that the above will fail. I've banged my head against that wall and finally realized that. The reason is that __myBlock is not within the scope of myRow's block of code. I fixed this by doing the following:

return myFunc.prototype.section1.__myBlock.$('label');

However, one of the methods I tried (and failed) was to pull the __myBlock into the myRow scope by doing the following:

myRow: {
    __myBlock: myFunc.prototype.section1.__myBlock,
    label: {
        return this.__myBlock.$('label');
    }
    // ...
}

My question is, is my working "fix" the best way to do this? Is there a better way to accomplish what I'm trying to do?

Machtyn
  • 2,982
  • 6
  • 38
  • 64
  • 1
    Have you tried `return this.section1.__myBlock.$('label');`? – gen_Eric May 20 '16 at 16:35
  • Objects don't have scope. You cannot refer to another *property* in the same object. The only way is to refer to the object *itself*, then get the property. – gen_Eric May 20 '16 at 16:36
  • I tried your suggestion and the intellisense in my IDE liked it, but the object comes back as `undefined`. Also, I couldn't think of a better way to describe my inability to reference said object and thought "scope" would have been a good choice of word. Is there a better way to describe that concisely? – Machtyn May 20 '16 at 16:41
  • hey, there is no way of this code work correctly, first you need to use `this` keyword like `this.__block`, but, you did put a hash object in your prototype that'll not point to the desired scope, i.e., the instances of the function, so you have to remove this hash of you prototype and move the methods inside it one level up... –  May 20 '16 at 16:55
  • When I refer to the full chain, it works. `label: function() { return myFunc.prototype.section1.__myBlock.$('label'); }` is working for me. – Machtyn May 20 '16 at 16:59
  • moving the __myBlock inside of the myRow hash is the only way to this code work... it is fine –  May 20 '16 at 17:02
  • Not explicitly stated in the OP, but the __myBlock will be needed for other hashes within section1. – Machtyn May 20 '16 at 17:15
  • You can't (and shouldn't) [nest prototype methods](https://stackoverflow.com/questions/15884096/organize-prototype-javascript-while-perserving-object-reference-and-inheritance). – Bergi May 20 '16 at 17:52
  • Yes, I should probably break out those "nested" methods into their own class. I had thought about that last night, but forgot today. Thanks. – Machtyn May 20 '16 at 18:34

0 Answers0