Attribute detection in a JS object

Source: Internet
Author: User
Tags hasownproperty

A problem was encountered in the recent project where the Window object was rendered when the page was loaded, an object obj was added to the window during rendering, a file was loaded, and an attribute para was added to obj, but the process was asynchronous.

You then need to detect if the window has an Obj object, and if the Obj object has the Para property. Because it is asynchronous. When performing this operation, it may not render to para, and if is required. else.. Perform another set of operations.

When you encounter a problem, how can I tell if the window object has an Obj object and if the Obj object has a Para property?

    1. The previous use of the ' obj ' in window judgment. But to take my big say this is not very good for the system performance, I hope I can be implemented in another way.

    2. After lookup, locate the hasOwnProperty operation using Window.hasownproperty (' obj ') && window.obj. hasOwnProperty (' Para '), which was judged.

      But there is a problem with this approach: you can only judge if your own property exists, and you cannot judge an inherited property.

    3. Use undefined judgment: window.obj!== undefined;

      You can use this method to determine your own properties and inheritance properties, but when the obj value is undefined, it fails.

    4. Use conditional statements to directly determine if (!! Window.obj.para).

      But using this method, if the property value is Undefined,null, "", 0, time, return false





Reference: http://www.3lian.com/edu/2013/05-20/70754.html

Attribute detection in a JS object

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.