Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vue中为啥能this 直接获取到data中的值 #78

Open
dcharlie123 opened this issue Jul 8, 2022 · 0 comments
Open

vue中为啥能this 直接获取到data中的值 #78

dcharlie123 opened this issue Jul 8, 2022 · 0 comments

Comments

@dcharlie123
Copy link
Owner

new Vue({
   .....
   data(){
     return {
       a:1
     }
   }
})
// 4762行
proxy(vm, "_data", key);

vm为全局Vue

 // sourceKey-> '_data'
  function proxy (target, sourceKey, key) {
    sharedPropertyDefinition.get = function proxyGetter () {
      return this[sourceKey][key]
    };
    sharedPropertyDefinition.set = function proxySetter (val) {
      this[sourceKey][key] = val;
    };
    Object.defineProperty(target, key, sharedPropertyDefinition);// target->vm,  key->'a'
  }

当我们访问this.a时程序会走Object.defineProperty(target, key, sharedPropertyDefinition)中的get返回this[sourceKey][key],即this['_data']['a'],设置值也同理

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant