Ext. apply: easy to understand

Source: Internet
Author: User
Apply: The apply and applyIf methods in Ext are used to apply attributes of an object to another object, which is equivalent to copying attributes. The difference is that apply will overwrite the properties in the target object, while applyIf will only copy the target object... syntaxHighlighter. all apply: the apply and applyIf methods in Ext are used to apply the attributes of an object to another object, which is equivalent to copying attributes. The difference is that apply will overwrite the properties in the target object, while applyIf only copies the properties in the target object but not in the source object. The signature of the apply method is "apply (Object obj, Object config, Object defaults): Object". This method contains three parameters. The first parameter is the target Object to be copied, the second parameter is the copied source object, and the third parameter is optional, indicating that a default value is provided to the target object. You can simply copy the attributes of the third parameter (if any) and the second parameter to the first parameter object. See the following code: var b1 = {p1: "p1 value", p2: "p2 value", f1: function () {alert (this. p2) }}; var b2 = new Object (); b2.p2 = "b2 value"; Ext. apply (b2, b1); b2.f1 (); in the above Code, Ext. the apply (b2, b1) Statement copies the attribute of b1 to the b2 object. Therefore, the message "p2 value" is displayed when you call the f1 method of b2. Although the b2 object already contains the p2 property value, the copied property value will be overwritten. You can specify the default value of the copy attribute in the third parameter when calling the apply method, for example, the following code: Ext. apply (b2, b1, {p3: "p3 value"}); alert (b2.p3); this will make b2 contain a p3 attribute with the value "p3 value ". The applyIf method has the same functions as apply, but does not copy the attributes existing in the target object and source object. For example, change the code that demonstrates the apply method to applyIf, as follows: Ext. applyIf (b2, b1); b2.f1 (); because the p2 attribute already exists in b2, this is referenced in b2.f1. in p2, we get "b2 value" instead of the "p2 value" defined in b1 ".
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.