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:
Http://stackoverflow.com/questions/171251/how-can-i-merge-properties-of-two-javascript-objects-dynamically
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