3

I have this simple code -

class Repo {
    constructor(someParam = null) {
        this.getDic = someParam;
    }
    static set getDic(ref){
        this.ref = ref;
    }
    static get getDic(){
        return this.ref;
    }
    static key(key){
        return this.getDic[key];
    }
}

let newRepo = new Repo({yeah: 'baby'});
Repo.key('yeah') // Uncaught TypeError: Cannot read property 'yeah' of undefined

Why is the 'getDic' getter undefined in the 'key' static method?

Thanks!

5
  • What do you believe static does? Commented Mar 17, 2016 at 13:24
  • @Bergi allows using the method/getter/setter without needing to instantiate the class... ? Commented Mar 17, 2016 at 13:33
  • 1
    Right. Then how do you expect the static method to access instance specific data? Commented Mar 17, 2016 at 13:40
  • @FelixKling You're absolutely right! Thank you! :) Commented Mar 17, 2016 at 13:41
  • Right. And that's the reason why static properties are accessed on Repo, not on newRepo (as you have now edited in). You most likely just don't want to use static at all. Commented Mar 17, 2016 at 13:49

2 Answers 2

4

when you define a static property in the class its bind it to the constructor so when you do new Repo() the return value not containing the static method but new Repo().constructor will contain the static methods

let newRepo = new Repo({yeah: 'baby'});
Repo.key // defined
newRepo.key // not defined
newRepo.constructor.key // defined

and another thing: the Repo.key === newRepo.constructor.key. So by creating a static property (like in other language such as c#) the property lives once in the scope, but this is a completely different issue :)

EDIT:

The reason that your code isn't working is because you are using a get property as a function.

By putting the set key before a function property you are setting the function to be a set property... that means that the function is been set a get on Object.definePropery

Lets look at the convert es6 using babel

'use strict';

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var Repo = function () {
    function Repo() {
        var someParam = arguments.length <= 0 || arguments[0] === undefined ? null : arguments[0];

        _classCallCheck(this, Repo);

        this.getDic = someParam;
    }

    _createClass(Repo, null, [{
        key: 'key',
        value: function key(_key) {
            console.log(this.constructor.getDic);
            return this.getDic[_key];
        }
    }, {
        key: 'getDic',
        set: function set(ref) {
            this.ref = ref;
        },
        get: function get() {
            return this.ref;
        }
    }]);

    return Repo;
}();

var newRepo = new Repo({ yeah: 'baby' });
Repo.key('yeah'); // Uncaught TypeError: Cannot read property 'yeah' of undefined

To make a long story shot just use Repo.key = 'heya' and you are good to go.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer, I've edited my question to better reflect the problem. Do you have any input on that? Thanks!
2

I had a very similar problem; it turns out this doesn't refer to the static class the way you'd expect. Using the actual classname fixes it. So:

class Repo {
    . . .
    static key(key){
        return Repo.getDic[key];
    }
}

By swapping out the this for the classname, it should work properly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.