4

I am currently reading out a language setting from the dom and setting it as a global Vue.js variable like this:

const language = document.querySelector('html').getAttribute('lang');
Vue.prototype.$language = language;

It works fine but I have also seen this as an example from a Vue.js event bus:

Object.defineProperties(Vue.prototype, {
    $bus: {
        get() {
            return EventBus;
        }
    }
});

What is the real difference between these two and is there possible a more preferred way of doing this?

3
  • 1
    Not really vue-relative, kinda dupe of: stackoverflow.com/questions/38961414/… Commented Jun 27, 2017 at 8:12
  • @Cobaltway in my case not having any configuration options would result in the same as simply setting the prototype directly? Or are the default settings still different? Commented Jun 27, 2017 at 8:17
  • 1
    I don't think there is any real difference if you are not using any of defineProperties options. It may be marginally faster to assign directly the prototype. Must be tested. Commented Jun 27, 2017 at 8:25

1 Answer 1

3

Object.defineProperties is can be used when you need to set property descriptors such as getters , setters ,read only etc .

But in your case using Vue.prototype.$language = language; will be the more cleaner approach.

If you are looking for Vue preferred way, this is the guide on adding instance properties to Vue instance.

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

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.