Speaking of the feature markers in js, I always feel that it is a bit strange. Why do you talk about this attribute? It originated from a question about a problem. We all know that the window object is actually
Since an instance of the browser window is an instance, this instance should have "properties" and "methods", such as the following:
When we use functions, we define some properties, such as name, age, etc., and we can also delete, set and update operations on them.
Then the following question is. Since the name of my person object can be modified dynamically, according to this principle, I should also be able to modify the undefined value under window, and then I am very interested in taking a look.
The result is still conceivable. I have not successfully modified the undefined value, so why is this happening? There is no reason. It is not the packaging type mentioned in the previous article, but we have to respect the result. This shows that the underlying js must have done something to cause such a result. In fact, in the underlying js, the Writable=false flag is used by default to make the undefined attribute unwritten. Let's take a look at it in detail below.
1: [Writable] Feature
When it comes to whether the attribute is writable, you may think of the get/set accessor in C# or the readonly keyword. Below you will definitely be interested in how I should do the read-only operation of the attribute. In js, you just need to use the defineProperty method.
From the above example, you can see three fun things:
<1>: I used the defineProperty method to turn person.name into a read-only field. Someone wants to say that this is obviously a method, but how can it be a feature? In my mind, the feature is
[xxx] mode, so this is the difference between js. After all, js does not have syntactic sugar, so the definition characteristics can only be defined through the underlying public method, that is, defineProperty.
<2>: I not only see writable in the method, but also a configurable. So what is this for? In fact, this is the basic configuration, telling the js engine whether it can delete and update attributes. When I set configurable to false, you cannot delete p.Name, because this will be an invalid operation.
After reading these, I think you should understand why undifined cannot delete or update. They are all made of weird marks, do you think it’s very interesting?
<3>: The next question worth thinking about is how the js engine does it. There is also Attribute in C#, and this Attribute will be recorded in metadata after being compiled by the compiler. Then you can use reflection to get any value in your Attribute. For example, the Serializable serialization class, which tells SerializableFormat how to reflect and read the class, which fields can be serialized, and which cannot, and you need to use NonSerialized tags.
The code copy is as follows:
[Serializable]
public class Test
{
[NonSerialized]
public string Name;
}
From the figure, we can see that the Name is notserialized in IL. We know that there is no reflection in js, so the breakthrough is naturally in the defineProperty method. Unfortunately, we cannot see what the underlying source code of this method is like, so we don’t know what it does to the Name field to dynamically make it unwritable. We can only guess the behavior that may occur in the js engine based on our understanding.