最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Understanding the 'this' keyword in Vue - Stack Overflow

programmeradmin1浏览0评论

I have started to learn VueJS from scratch. I am following their official Guide. But I am stuck here:

In this example...

var app5 = new Vue({
  el: '#app-5',
  data: {
    message: 'Hello Vue.js!'
  },
  methods: {
    reverseMessage: function () {
      this.message = this.message.split('').reverse().join('')
    }
  }
})

..how is it that the message property is directly being accessed without any reference to the data object? If this keyword refers to the current Vue instance, shouldn't the message property be accessed like this: this.data.message?

Consider the following example:

({
  name: "John Doe",
  data: {
    message: "Hello World"
  },
  greet: function(){
    console.log("I am " + this.name);
    console.log("I have a message for you: " + this.data.message); //see here
  }
}).greet();

This is how I would have accessed a property in vanilla javascript. Can someone please make me understand what's going on behind the scenes?

I have started to learn VueJS from scratch. I am following their official Guide. But I am stuck here: https://v2.vuejs.org/v2/guide/#Handling-User-Input

In this example...

var app5 = new Vue({
  el: '#app-5',
  data: {
    message: 'Hello Vue.js!'
  },
  methods: {
    reverseMessage: function () {
      this.message = this.message.split('').reverse().join('')
    }
  }
})

..how is it that the message property is directly being accessed without any reference to the data object? If this keyword refers to the current Vue instance, shouldn't the message property be accessed like this: this.data.message?

Consider the following example:

({
  name: "John Doe",
  data: {
    message: "Hello World"
  },
  greet: function(){
    console.log("I am " + this.name);
    console.log("I have a message for you: " + this.data.message); //see here
  }
}).greet();

This is how I would have accessed a property in vanilla javascript. Can someone please make me understand what's going on behind the scenes?

Share Improve this question edited Jul 14, 2022 at 0:51 tony19 138k23 gold badges276 silver badges346 bronze badges asked Apr 19, 2017 at 20:46 TanmayTanmay 3,15910 gold badges56 silver badges89 bronze badges 3
  • 3 this isn't always bound lexically. It can also be bound with call, apply, or bind, which is likely what VueJS is doing. – castletheperson Commented Apr 19, 2017 at 20:53
  • 3 The object you pass to new Vue() and the object it returns are not the same object. You pass it an object it uses to create the Vue. Part of that process is making the data properties, methods, computeds, etc available as properties of the created object. – Bert Commented Apr 19, 2017 at 20:56
  • 5 Basically It has proxy that makes this.$data.property accesible as this.property – Belmin Bedak Commented Apr 19, 2017 at 21:00
Add a comment  | 

3 Answers 3

Reset to default 6

In Vue, Vue instance proxy properties of data and methods by using Proxy

Read this: Options / Data

From that we get "The data object for the Vue instance. Vue will recursively convert its properties into getter/setters to make it “reactive”." Meaning everything in the data object property is applied directly to the new Vue. This makes thos properties available on this as getters and setters.

this is special object. When compiled, (yes, it is compiled!) the data keyword would disappear. All the properties and methods will wrap to a new object, so this can now conform to the JS rules.

发布评论

评论列表(0)

  1. 暂无评论