Reference another field inside a javascript object

Viewed 10208

I have an object in javascript:

admins: {
    articles: {
        path: '/admins/articles',
        template: '/views/admins/articles.html',
        link: function() {
            return path;  // !!! how to reference the 'path'?
        }
    }
}

I have a lot of objects like this, and each of them has a path field and a link function. I want to use the field path in link, but I can't just use path.

What should I do?

5 Answers

I'll just point out that you don't want to use ES6 fat arrow here, because there will be no this pointer in that case:

var someObj = {
    admins: {
        articles: {
            path: '/admins/articles',
            template: '/views/admins/articles.html',
            link: () => {
                return this.path; // 'this' is undefined
            }
        }
    }
};

someObj.admins.articles.link() === undefined

Related