Dynamic selection of JavaScript objects and _javascript techniques of traversal objects

Source: Internet
Author: User
(i) Dynamic selection methods and properties

In practice, we often encounter situations where we invoke one of the two methods [1] According to a certain condition, or read and write on one of the two properties [2]. The following code shows this scenario:
Copy Code code as follows:

if (condition) {
MYOBJ.METHOD1 (SOMEARG);
} else {
MYOBJ.METHOD2 (SOMEARG);
}

JavaScript provides a simple syntax for dynamically selecting methods and properties using the square bracket operator ([]). As the following code shows, JavaScript has two equivalent member access syntax (this feature is common in dynamic languages):
Obj[expressionresultinginmembername] = = Obj.membername

If you've ever used an integer subscript to access an element in an array, you've already started using the bracket operator for dynamic member selection. This is because the array object itself contains the attributes (and the length property) that are named after the number subscript. However, JavaScript does not allow you to use the dot operator (.) To access these properties directly, so myarray.0 is syntactically illegal (which, sadly, would have been a pretty cool syntax).
Why is the square bracket operator more powerful than the dot operator notation? This is because you can access the members of an object in square brackets using any content that represents the name of the member. These include literal values, variables that hold member names, combinations of names (in most cases concatenation of strings), and ternary operators (condition?). Valueiftrue:valueiffalse) Implementation of the fast If/then selection. All of this content will be processed into a string, and JavaScript will use this string to find the corresponding member.
Because a function in JavaScript itself is also an object, it can be referenced like any other value. If the result of an expression is a function, you can call it directly with the parentheses operator, just as you would call a function directly with the function name.
It should be noted that if you use such techniques extensively on the parameters passed to the method, the confusing parentheses may make the code unreadable, and it is wiser to use the regular if/else structure.

(ii) JavaScript traversal object properties and methods

JavaScript uses the for in statement to traverse the properties and methods of an object. The for in statement loops through the JavaScript object, and each time the loop gets a property or method of the object.

Grammar:
Copy Code code as follows:

For (ValueName in objectname) {
Code
}

Where ValueName is the variable name, the name of the property or method is saved, and the value of the valuename is changed for Each loop.
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.