javascript - $.extend property not working -
recently discovered $.extend , applied on code:
var chartfactory = function(options) { this.defaults = { type: "line", width: "800", height: "500", heightscale: { domain: ["0", "250"], range: ["300", "0"] }, widthscale: { domain: ["0", "250"], range: ["0", "900"] }, yaxis: { ticks: ["5"], scale: "heightscale", orient: "left" }, xaxis: { ticks: ["10"], scale: "widthscale", orient: "bottom" } }; $.extend(this.options, options, this.defaults); }; i have chartfactory class linechart object of it.
edit: trying merge contents of object values default values such values of new object has been made retained , resultant should on new object.
var linechart = new chartfactory({ type: "line", xaxis: { ticks: ["20"], scale: "widthscale", orient: "bottom" } }); so when consoled linechart(console.log(linechart),this came up: 
the linechart.xaxis.ticks should have been "20".
what doing wrong?
you have create this.options first, merge other 2 objects object, passing this.defaults first, options, latter overwrite properties in former
var chartfactory = function(options) { this.defaults = { type: "line", width: "800", height: "500", heightscale: { domain: ["0", "250"], range: ["300", "0"] }, widthscale: { domain: ["0", "250"], range: ["0", "900"] }, yaxis: { ticks: ["5"], scale: "heightscale", orient: "left" }, xaxis: { ticks: ["10"], scale: "widthscale", orient: "bottom" } }; this.options = {}; $.extend(this.options, this.defaults, options); }; var linechart = new chartfactory({ type: "line", xaxis: { ticks: ["20"], scale: "widthscale", orient: "bottom" } });
Comments
Post a Comment