简体   繁体   English

将对象文字转换为可重用类?

[英]Turn Object Literal into Reusable Class?

Is it possible to take this code below and make it so that it is easily reusable without having to copy and paste each time I want to create a new options: 是否可以在下面使用此代码并将其设置为使其易于重用,而不必每次我要创建新选项时都进行复制和粘贴:

optionA: {
  chart: {
    type: 'stacked',
    color: 'red',
    showControls: false
  }
}

Now if I wanted to have another chart, I have to do this right now: 现在,如果我想要另一个图表,则必须立即执行以下操作:

optionB: {
  chart: {
    type: 'line',
    color: 'yellow',
    showControls: false
  }
}

It would be nice if there would be a way to create a class or something of sorts so that I can add a new option without having to reuse all of the same code, kinda like hiding everything that does not need to be known for the implementation. 如果有一种方法可以创建一个类或某种某种形式,这样我就可以添加一个新选项而不必重用所有相同的代码,这有点好,就像隐藏了实现时不需要知道的所有内容一样。 。 I have looked around for ways to do this but my searches have had no success. 我一直在寻找实现此目的的方法,但是搜索没有成功。

EDIT: I might not have been enough information after looking at the responses so far. 编辑:到目前为止,我可能没有足够的信息。 Here is the full data: 这是完整的数据:

timelineA: {
    chart: {
        type: 'stackedAreaChart',
        color: defaultColor,
        showControls: false,
        showLegend: false,
        useInteractiveGuideline: true,
        margin: margin,

        x: AnalysisTransforms.xValue,
        y: function(d){
            return d[1];
        },

        forceY: [0, 1],

        noData: $scope.translations.noData,
        xAxis: {
            showMaxMin: false,
            tickPadding: 16,
            tickFormat: AnalysisTransforms.formatDate,
            tickValues: AnalysisTransforms.xAxisTicks
        },
        yAxis: {
            tickPadding: 16,
            tickFormat: AnalysisTransforms.formatInteger
        }
    }
}

timelineB: {
    chart: {
        type: 'line',
        color: defaultColor,
        showControls: false,
        showLegend: false,
        useInteractiveGuideline: true,
        margin: margin,

        x: AnalysisTransforms.formatLine,
        y: AnalysisTransforms.linex,

        noData: $scope.translations.noData,
        xAxis: {
            showMaxMin: false,
            tickPadding: 16,
            tickFormat: AnalysisTransforms.formatParseDate,
            tickValues: AnalysisTransforms.xticks
        },
        yAxis: {
            tickPadding: 16,
            tickFormat: AnalysisTransforms.formatInteger
        }
    }
}

This is from angular-nvd3 and there is not enough support from the repo for me to figure this out. 这是来自angular-nvd3的,在仓库中没有足够的支持让我知道。 I have started playing around with creating a class for this but don't think I am following best practices because I don't know how to convert object literal like this into a class that I can new up for any charts I want to create. 我已经开始为此创建一个类,但是我认为我没有遵循最佳实践,因为我不知道如何将这样的对象文字转换为可以为要创建的任何图表创建的类。

What I see is that there is some code that is used between the 2 and then some that is not. 我看到的是,在2和2之间使用了一些代码,而没有使用。 I hope I am not confusing beyond the ability for help in this issue. 我希望我在这个问题上不会感到无所适从。

Something as: 如:

var defaults = {
  color: defaultColor,
  showControls: false,
  showLegend: false,
  useInteractiveGuideline: true,
};

function buildChart(type, opts) {
  return Object.assign({
    type: type,
  },
  defaults,
  opts);
}

// Then you can do variations as 
var lineChart = buildChart('line', { color: '#CCC' });
var barChart = buildChart('bar', { color: 'red' });

In Object.assign it's important for opts to go last, so it can override the defaults when required. Object.assign ,最后opts很重要,因此可以在需要时覆盖defaults

  function getObj(opts) {
     return {
        type: opts.type,
        colour: opts.colour,
        showControls: opts.showControls
     }
  }

  var b = getObj({type: 'sometype'})

You could add some defaults in if you wanted, but the above should work 您可以根据需要添加一些默认值,但是上面的应该可以

Yes you can create object easily...if you work on recent browser there is a nicer way to do it. 是的,您可以轻松地创建对象...如果您使用的是最近的浏览器,那么还有一种更好的方法。 but this is just a sample to start you 但这只是一个示例

var Option = (function ()
{
     var Class = function (Type, Color)
     {
        this.chart = {
        type: Type,
        color: Color,
        showControls: false
      };
};

    (Class.prototype);
    return Class;
})();

var optionA = new Option('stacked', 'red');
var optionB = new Option('line', 'yellow');

here a Fiddle 这里是小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM