The premise

Hi, everyone. Here we go again. Reactive in the source code, reactive in the source code, reactive in the source code, reactive in the source code, reactive in the source code, reactive in the source code, reactive in the source code, reactive in the source code, reactive in the source code, reactive in the source code. Good nonsense no more to say, we began!!

doubt

You may already be working on VUE3, or do you know how Reactive works when asked? Do you know how ref is implemented you’ve probably heard that ref is implemented by defineProperty, but if you look at the source code, you see that? Which of these words can be seen using defineProerty? This relates to the result of class compilation. Here is the result

Question answer

  1. In es6, class is defined property, so ref is defined property

Source code parsing begins

  1. In case you haven’t used ref before, here is a simple example of how to use ref. If you still don’t understand it, you can read the official documentation or leave a comment and ask me

  1. The createRef method that we call ref will eventually return an instance of ref

If (isRef(rawValue)) = ref (rawValue); if(isRef(rawValue)) = ref (rawValue);

  1. Above is the class {RefImpl}. When new is executed, the constructor constructor is executed, and the assignment is performed within the constructor
  2. The constructor contains code {this._value = _shallow? _rawValue: convert(_rawValue)} _shallow: convert(_rawValue
  3. The internal get values and set values are actually property accessor patterns, and if you don’t know, you can read more about design patterns, and actually collect and trigger dependencies when you get and set

  1. If you are careful, you may have noticed that I did not explain what the {convert} method does
  2. In principle ref is designed to handle simple data types, but if you want to pass object types, we can do that, but reactive is called internally

At the end

Well, ref is also relatively simple, but I think it’s important to understand the class conversion result, that’s the most important thing. So without further ado, look forward to the next analysis