JavaScript Tour-sixth stop: see writable features

Source: Internet
Author: User

Talking about the characteristics of JS in the mark, always feel a bit strange, then why to talk about this attribute, originated in a question, we all know that the Window object is actually

An instance of the browser window, since it is an instance, should have "attributes" and "methods", such as the following:

When we use function, we define properties such as Name,age and so on, and can also perform delete,set and update operations on them.

So here's the question, since the name of my person object can be dynamically modified, in this case, I should be able to modify the undefined value under window, and then

Very interested to have a look.

The result is still conceivable, I did not successfully modify the value of undefined, then why this? It doesn't make sense, it's not the type of packaging that was mentioned in the last article, but we have to respect the results,

This means that JS bottom must be done what will lead to such results, in fact, the bottom JS default with the Writable=false tag to let the undefined attribute is not writable, the following specific look.

One: [Writable] Features

When it comes to whether a property is writable, maybe you think of the get/set accessor in C # or the ReadOnly keyword, below you'll definitely be interested in how I should do the read-only operation of the property.

In JS you just have 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, and someone has to say, this is the method, how it is a feature, the character in my mind is

"XXX" mode, so this is the difference between JS, after all, JS does not have syntax sugar, so the definition of features can only be defined by the bottom-level public method, that is, DefineProperty.

<2>: I see not only writable in the method, but also a configurable, what is this? In fact, this is the basic configuration, tell the JS engine can delete,update

property, when I set configurable to False, you cannot delete p.name because this is an invalid operation.

After reading these I think you should understand, why undifined can't delete and update. It's all a sign, isn't it funny?

<3>: The next question to consider is how the JS engine does it, in C # There is also Atrribute, and this atrribute is compiled by the compiler will be logged to the meta-data

, then you can confidently use reflection to get any value in your atrribute, such as Serializable serialization class, which tells Serializableformat

How to reflect the Read class, which fields can be serialized, which can not, can not you need to use the nonserialized tag.

1     [Serializable]2      Public class Test 3     {4        [nonserialized]5public         string  Name; 6     }

You can see that the IL has been recorded in the name of the notserialized feature, we know that JS does not reflect, so the breach is naturally in the DefineProperty method inside, but

Is more regrettable, we do not see this method of the underlying source of the exactly how it is, so we do not know what it is to name this field exactly what to do to the dynamic cause it is not writable, only

Can be based on understanding to guess the JS engine can occur behavior.

JavaScript Tour-sixth stop: see writable features

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.