Javascript------Reflection Mechanism

Source: Internet
Author: User

Speaking of reflection, in fact many languages such as Java and. NET has the mechanism of reflection, see this tall noun, you will certainly think this is what thing. Is reflection something that is not a mapping of something?

No.

We can interpret the reflection as plain as: when you are dealing with an unknown object (you don't even know a property or API of the object), pry into its internal structure, get its internal properties and methods, and that's the reflection mechanism.

OK, how do we spy on the object's internal structure?

For (var prop in obj) {console.log (prop); or the other statement}

What is the purpose of getting each property or method, we are going to spy on an object, to use its things, what we do is meaningful.

For (var prop in obj) {if (typeof obj[prop] = = = ' function ') {obj[p] ();   Methods to implement}else{Console.log (Obj[p]); property on Output}}

So that we can use the property or the method inside, so that the reflection mechanism appears to be meaningful.

Example: Assigning a property of an object obj1 to a obj2, requiring that the more than obj1 attribute in the Obj2 not be washed away, and if there are duplicate attributes, it is overwritten.

For (Var prop in obj1) {
Obj2[prop] = Obj1[prop];
}

Console.log (OBJ2); This allows the OBJ2 to save its own attributes to the latest values and to increase the new properties of Obj1.

This article is from the "8129266" blog, please be sure to keep this source http://8139266.blog.51cto.com/8129266/1835762

Javascript------Reflection Mechanism

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.