Javascript Component Property Merging

Source: Internet
Author: User

Why do we need to merge the attributes of two components? When developing components, users PASS Parameters in the form of JSON objects. You are going to open 10 parameters, but the user may only pass 5. YourCodeIf you access the remaining five attributes, the notdefined error will occur. You cannot determine whether a specific attribute is defined internally. To save trouble, define a preset parameter component and merge it with the parameter component passed in by the user.

For example:

// Define the component ============================ yjcurvedefaultoption ={ rawdata: NULL, renderto :"", xaxistitle: "", xaxisunit: "", yaxistitle: "", yaxisunit: "", charttitle: "", xaxisisdatetime: false} yjcurve = function (option) {// merge the parameters and preset parameters given by the user. If the parameters are not merged, an error option = $ may be returned when unspecified properties are accessed later. extend ({}, yjcurvedefaultoption, option); Build (); function build (){}}
// Use the component ============================ var rawdata1 = [[2.5, 12907]; vaR C1 = new yjcurve ({xaxisdatetime: false, rawdata: rawdata1, xaxistitle: "shotcount", renderto: "div1", charttitle: "mold temperature", yaxistitle: "temperature ", yaxisunit: "℃ "});


How to merge the attributes of two components is discussed in detail here:


Jquery provides the following functions:

// Merge options object into settings objectvar settings = {validate: false, limit: 5, name: "foo"}; var Options = {validate: True, Name: "bar"}; jquery. extend (settings, options); // now the content of settings object is the following: // {validate: True, limit: 5, Name: "bar"} var defaults = {validate: false, limit: 5, name: "foo"}; var Options = {validate: True, name: "bar "}; /* merge defaults and options, without modifying defaults */var settings = $. extend ({}, defaults, options); // The content of settings variable is now the following: // {validate: True, limit: 5, Name: "bar"} // defaults and options variable remained the same


Functions in hightcharts. SRC. JS:

 function Merge () {var I, Len = arguments. length, ret = {}, docopy = function (copy, original) {VaR value, key; For (key in original) {If (original. hasownproperty (key) {value = original [Key]; // an object is replacing a primitive if (typeof copy! = 'Object') {copy ={};}// copy the contents of objects, but not arrays or DOM nodes if (Value & typeof value = 'object' & object. prototype. tostring. call (value )! = '[Object array]' & typeof value. nodetype! = 'Number') {copy [Key] = docopy (copy [Key] | |{}, value ); // primitives and arrays are copied over directly} else {copy [Key] = original [Key] ;}} return copy ;}; // For each argument, extend the return for (I = 0; I 
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: 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.