2015 37th Friday JavaScript function arguments object skillfully with a

Source: Internet
Author: User

A clever use of JavaScript functions: Assuming that the action has a Jsonobject type of object data, its value may be empty, the foreground JSP page in the JS code would like to directly through the El expression, that is, the form of ${data} to access the value of the object, May cause JS preprocessing error because the data object is empty, such as the JSP has such code, VAR obj=${data}, when the background data is null, the foreground page will become Var obj=, and the page error is terminated. Some people want to solve the problem by a similar var obj= (' ${data} ' = = ' {}:${data}), but the code is still compiled, because the preprocessing becomes var obj= (' = = '? {}:), direct syntax error, this situation is generally only through ' ${data} ' Give this value to a string and then convert it to a JSON object through the eval () function or the introduction of a third-party JSON library, which can also be resolved by the following function:

function Getjsondata (data) {

if (arguments.length>0) {

return data;

}

return {}

}

var obj = Getjsondata (${data});

2015 37th Friday JavaScript function arguments object skillfully with a

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.