Improved switch equivalent comparison

Source: Internet
Author: User

This method is used to replace the Equal Value Comparison in the switch.

Generally, each comparison of the same value needs to be judged repeatedly.

The use of user-defined functions avoids repeated judgment and improves the efficiency.

The principle is simple: use an object to locate the value in the desired range.

The returned value can be a variable, an array, or a function.

Copy to ClipboardReference content: [www.bkjia.com] // The Switch is equivalent.
// Pass the parameter: The variable to be compared, the JSON format of the comparison value and return value
// It is more appropriate to set a default return value 'default '.
$ SEql = function (X, o, f, undefined ){
Return (f = o [X])! = Undefined? F: o ['default']
}

// Call example

GetX = function (C) {return $ SEql (C, {'-1': 100,0: 140,1: 187,2: 267,3: Random, 4: 427,5: 507,6: 587,7: 667,8: 747,9: 827,10: 865,11: 950})}; // returns the checkpoint X based on the column

GetY = function (R) {return $ SEql (R, {0: 75,1: 175, 2: 270,3: Random, 4: 470,5: 575})}; // returns the Y coordinate based on the row, used to coordinate the bottom and foot of a zombie

GetY1Y2 = function (R) {return $ SEql (R, {0: [86,180], 1: [181,280], 2: [281,385], 3: [], 4: [386,475], 5: [476,600]})}; // return the range of Y based on the row.

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.