简体   繁体   English

如何用JavaScript中的函数定义对象?

[英]How to define objects with a function in javascript?

I have three identical objects that I'd like to define in three slightly different ways. 我想用三种略有不同的方式定义三个相同的对象。 The only difference is that I'm accessing a different property within my source data (via Bracket Notation) for each object as shown below 唯一的不同是,我正在通过每个对象的源数据(通过括号表示法)访问不同的属性,如下所示

The object I'm using is not a function. 我正在使用的对象不是函数。 For example Object1 is defined by 例如,Object1定义为

var Object1  = dc.lineChart("#chart-line-hitsperday"); 


var D = "Dimension1"
Object1
    .width(200).height(200)
    .dimension(SourceData.dimension(function (d) {return d[D];}))
    .group(SourceData.dimension(function (d) {return d[D];}).group().reduceSum(function (d) {return d.Amount;}))


var D = "Dimension2"
Object2
    .width(200).height(200)
    .dimension(SourceData.dimension(function (d) {return d[D];}))
    .group(SourceData.dimension(function (d) {return d[D];}).group().reduceSum(function (d) {return d.Amount;}))


var D = "Dimension3"
Object3
    .width(200).height(200)
    .dimension(SourceData.dimension(function (d) {return d[D];}))
    .group(SourceData.dimension(function (d) {return d[D];}).group().reduceSum(function (d) {return d.Amount;}))

Is there a way that I can define each with 1 line of code as oppose to three lines of code. 有没有一种方法可以定义每行1行代码,而不是三行代码。 In other words, I'd like to convert above into the following 换句话说,我想将以上转换为以下内容

Object1.definition("Dimension1")
Object2.definition("Dimension2")
Object3.definition("Dimension3")

by defining 'definition' as something like : 通过将“定义”定义为:

definition(D) =         
    .width(200).height(200)
    .dimension(SourceData.dimension(function (d) {return d[D];}))
    .group(SourceData.dimension(function (d) {return d[D];}).group().reduceSum(function (d) {return d.Amount;}))

Is this possible? 这可能吗?

See JSFiddle Here: http://jsfiddle.net/chrisguzman/juhaoem2/ 在此处查看JSFiddle: http//jsfiddle.net/chrisguzman/juhaoem2/

I've tried the following with no luck: 我已经尝试了以下方法,但是没有运气:

var definition = function(D){     
    this.width(200).height(200)
    .dimension(SourceData.dimension(function (d) {return d[D];}))
    .group(SourceData.dimension(function (d) {return d[D];}).group().reduceSum(function (d) {return d.Amount;}))
}


Object1.definition("Dimension1")

Second Try 第二次尝试

Object1.prototype.definition = function(dim){
    this.width(200).height(200)
        .dimension(SourceData.dimension(function (d) {return d[dim];}))
        .group(SourceData.dimension(function (d) {return d[dim];}).group().reduceSum(function (d) {return d.Amount;}))
};

Try this: 尝试这个:

SuperObject.prototype.definition = function(dim){
    this.width(200).height(200)
        .dimension(SourceData.dimension(function (d) {return d[dim];}))
        .group(SourceData.dimension(function (d) {return d[dim];}).group().reduceSum(function (d) {return d.Amount;}))
};

Also you should define the following functions as well: 另外,您还应该定义以下功能:

SuperObject.prototype.width()
SuperObject.prototype.height()
SuperObject.prototype.dimenstion()
SuperObject.prototype.group()

Or put them in the SuperObject prototype chain. 或将它们放在SuperObject原型链中。

Note that SuperObject is the constructor of Object1 , Object2 ,... 请注意, SuperObjectObject1Object2 ,...的构造函数。

UPDATE #1 更新#1

Usage: 用法:

var obj = new SuperObject();
obj.definition("Dimension1");

Using this ? 使用this

definition = function(D){     
    this.width(200).height(200)
    .dimension(SourceData.dimension(function (d) {return d[D];}))
    .group(SourceData.dimension(function (d) {return d[D];}).group().reduceSum(function (d) {return d.Amount;}))
}

Use: 采用:

definition.call(Object1,"Dimension1");

The code as you posted is highly unreadable so I'll try with a simpler example: 您发布的代码非常不可读,因此我将尝试一个简单的示例:

var o1={},o2={},o3={}
,dimensions = ['Dimension1','Dimension2','Dimension3']
,objects=[o1,o2,o3]
,len=dimensions.length,i=-1;
while(++i<len){
  //function returning function creating a closure
  //  in a while loop
  objects[i].somefn=(function(d){
    return function(){
      console.log(d);
    }
  }(dimensions[i]));
  objects[i].sameForAll='anything';
}
o1.somefn();
o2.somefn();
o3.somefn();

When a variable name starts with a capital it usually means it's a constructor so I'd advice not to use capitals unless they are all caps to indicate they are constants. 当变量名以大写字母开头时,通常意味着它是一个构造函数,因此,我建议不要使用大写字母,除非它们全部使用大写字母表示它们是常量。

[update] [更新]

Here is what it looks like applied to your code: 这是应用于您的代码的样子:

//wrap in IIFE
(function(dimensions,objects){
  var i=-1,len=dimensions.length;
  //for every object and dimension
  while(++i<len){
    //wrap in IIFE for closure
    (function(object,D){//see later what object and D is
      object
        .width(200).height(200)
        .dimension(
          SourceData.dimension(
            function (d) {
              return d[D];
            }
          )
        )
        .group(
          SourceData.dimension(
            function (d) {
              return d[D];
            }
          )
          .group()
          .reduceSum(
            function (d) {
              return d.Amount;
            }
          )
        );
    }(objects[i],dimensions[i]));//pass object and D
  }
}(['Dimension1','Dimension2','Dimension3'],[Object1,Object2,Object3]))

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

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