Javascript Writable features Introduction _javascript tips

Source: Internet
Author: User
Tags reflection

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

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

When we use function, we define attributes such as name,age and so on, and they can be delete,set and update operations.

So here's the question, and since the name of my person object can be dynamically modified, I should also be able to modify the undefined value under window, and then be interested to have a look.

The result is still conceivable, I did not successfully modify the value of undefined, then why this? No reason ah, it is not the last piece of the packaging type, but we have to respect the results, this shows that JS Bottom is sure to do what will lead to such results, in fact, at the bottom of JS default with the writable=false tag to let undefined properties can not write, the following specific look.

One: [Writable] Features

When it comes to whether a property can be written or not, you might associate it with a get/set accessor in C # or a readonly keyword, and you'll be interested in how I should do the read-only operation of the attribute. In JS you just have to use the DefineProperty method.

From the example above you can see three points of fun:

<1>: I used the DefineProperty method to turn Person.name into a read-only field, and someone said, this is obviously a method, how it is a feature, the characteristics in my mind is

"XXX" mode, so this is the difference between JS, after all, JS does not have grammatical sugar, so the definition of characteristics can only be through the bottom of the public way to define, that is, DefineProperty.

<2>: I have not only seen 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 properties, when I set the configurable to false, you can not delete the p.name, because this will be an invalid operation.

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

<3>: The next worthwhile question is how JS engine is done, in C # there is Atrribute, and this atrribute compiled by the compiler will be recorded in the metadata, Then you can confidently use reflection to get any value in your atrribute, such as the Serializable serialization class, which is to tell Serializableformat how to reflect the reading class, which fields can be serialized, which cannot, No, you need to use the nonserialized tag.

Copy Code code as follows:

[Serializable]
public class Test
{
[NonSerialized]
public string Name;
}

From the diagram can see that the IL has been recorded in the name of the notserialized feature, we know that there is no reflection in JS, so the breakthrough naturally in the DefineProperty method, but more regrettable, we do not see this method of the bottom of the source code in the end is what kind of, So I do not know what it is the name of the field to do what can be dynamically caused by it is not writable, can only be based on the understanding of the JS engine can be expected to occur behavior.

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.