I am trying to automate a particular module in our JS library and am stuck at a point where I want to define a set of properties (lets say an object that goes as construction parameter of a class).

我试图在我们的JS库中自动化一个特定的模块,并且我想要定义一组属性(假设一个对象作为类的构造参数)。

/**
 * This function initiates world peace!
 * @constructor
 * @param {object}  defaults        - The options to initiate peace.
 * @param {number}  defaults.issues - The number of issues being taken up.
 * @param {string}  defaults.source - The name of the location where process starts.
 */
 var WorldPeace = function (defaults) {
     // code here
 };

It is well and good had all properties of the construction was defined at one location. Unfortunately, my code has a number of modules contributing to that construction properties. Lets say, at some other portion of the code (in a later file) causes to have a couple of more properties

如果在一个地方定义了建筑的所有属性,那就很好了。不幸的是,我的代码有许多模块有助于构造属性。可以说,在代码的某些其他部分(在后面的文件中)导致具有更多属性

 * @param {Date} defaults.start  - The date when the process started.
 * @param {Date} defaults.stop   - The date when the process should stop.

How do I go about adding to the original set of properties that I had previously defined for WorldPeace function? Doing something like a mixin or subclassing the properties would be going overboard! As such, if I can simply inject to a property list definition it would be great.

如何添加我之前为WorldPeace函数定义的原始属性集?做一些像mixin或子类化属性的东西将会过火!因此,如果我可以简单地注入属性列表定义,那就太棒了。

2 个解决方案

#1


1

The easiest method is to use a record type:

最简单的方法是使用记录类型:

/**
 * This function initiates world peace!
 * @constructor
 * @param {{issues: number, source: string}} defaults - options to initiate peace.
 */
var WorldPeace = function (defaults) {
  // code here
};

You could also implement an interface:

您还可以实现一个接口:

/** @interface */
var WordPeaceDefaults;

/** @type {number} */
WorldPeaceDefaults.prototype.issues;

/** @type {string} */
WorldPeaceDefaults.prototype.source;

/**
 * This function initiates world peace!
 * @constructor
 * @param {WorldPeaceDefaults} defaults - options to initiate peace.
 */
var WorldPeace = function (defaults) {
  // code here
};

/**
 * @constructor
 * @implements {WorldPeaceDefaults}
 */
function MyWorldPeaceDefaults() {}

/** @type {number} */
MyWorldPeaceDefaults.prototype.issues = 0;

/** @type {string} */
MyWorldPeaceDefaults.prototype.source = '';

WordPeace(new MyWorldPeaceDefaults);

更多相关文章

  1. AngularJS:TypeError:无法读取未定义的属性'get'?
  2. 自定义ComboBox焦点不会切换到选择elem并且eventBubble不会停止
  3. javascript类定义与对象的性能
  4. 当函数在单独的PHP文件中定义时,调用JavaScript函数onclick按钮事
  5. python 修改vs工程属性
  6. MPRIS + Python (dbus):读写属性
  7. Python中类的使用(5私有属性)
  8. Python NameError:全局名称“Form”没有定义pyqt
  9. 'module'对象没有属性'views' django错误

随机推荐

  1. php+nginx实现自动化部署脚本(简单版)
  2. php 踩坑 PDO foreach bindParam
  3. php 性能分析工具 xhprof
  4. PHP根据身份证号码,获取性别、获取生日、
  5. php + ajax实现帖子点赞功能
  6. nginx和php-fpm通信,使用unix socket还是
  7. OneinStack 安装 PHP 扩展
  8. PHP判断点是否在多边形区域内外
  9. PHP 学习总结之函数
  10. php安装amqp扩展(windows)