Dynamic Selection and traversal of JavaScript objects

Source: Internet
Author: User

This article introduces two aspects of JavaScript objects: dynamic selection of methods and attributes, and traversal of Object Attributes and methods. For more information, see

(1) dynamic selection of methods and attributes in actual work, we often encounter this situation: one of the two methods [1] is called according to a certain condition, or perform read/write operations on one of the two attributes [2. The following code shows the situation: if (condition) {myObj. method1 (someArg);} else {myObj. method2 (someArg);} JavaScript provides a simple syntax to dynamically select methods and attributes using the square brackets operator. As shown in the following code, JavaScript has two equivalent member access syntaxes (this feature is common in dynamic languages): obj [expressionResultingInMembername] = obj. memberName if you used an integer subscript to access an element in the array, you have begun to use the square brackets operator for dynamic member selection. This is because the array object itself contains the attributes (and length attributes) named by the number subscript ). However, JavaScript does not allow you to directly access these attributes using the dot operator (.). Therefore, myArray.0 is invalid in syntax (sorry, this is a cool syntax ). Why is the square brackets operator more powerful than the dot operator notation? This is because you can use any content in square brackets that represents the member name to access the member of the object. These include literal, variables that store Member names, name combinations (in most cases String concatenation), and condition? ValueIfTrue: valueIfFalse) enables quick if/then selection. All the content will be processed into a string, and JavaScript will use this string to find the corresponding member. Because functions in JavaScript are also objects, they can be referenced like other values. If the result of an expression is a function, you can call it directly using the brackets operator, just as you call a function directly using the function name. It should be noted that if you use a lot of such techniques in parameters passed to the method, confusing parentheses may make the code hard to read, in this case, it is more wise to use the regular if/else structure. (2) JavaScript traverses object attributes and Methods. JavaScript uses the for in statement to traverse object attributes and methods. The for in statement cyclically traverses JavaScript objects. Each loop gets an attribute or method of the object. Syntax: the code is as follows: for (valueName in ObjectName) {// code} Where valueName is the variable name and stores the name of the attribute or method. The value of valueName changes every cycle.

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.