简体   繁体   English

Javascript对象选项:function或null

[英]Javascript object options: function or null

This question has probably been asked before, but I don't really know the proper keywords to find a solution on Google, so all my researches returned 0 :) Or better, all my researches ended up with "optional parameters" but for functions. 之前可能已经提出过这个问题,但我真的不知道在Google上找到解决方案的正确关键字,所以我所有的研究都返回0 :)或者更好的是,我所有的研究都以“可选参数”结束,但是对于函数来说。

Let's assume I have this class: 我们假设我有这门课程:

var Class = function (params) {
    // I get the data somehow
    params.name(data);
}

The problem is that not every time I instantiate new Class() I need also to set name() like: 问题是不是每次我实例化new Class()我都需要设置name()如:

new Class({
    name: function (data) {
        // Do something with the data
    }
    ... other params ...
})

But, with the above syntax, if I don't declare name parameter as a function, an error params.name is not a function is thrown: 但是,使用上面的语法,如果我没有将name参数声明为函数,则抛出一个错误params.name is not a function

// Error
new Class({
    ... other params ...
})

My question is: is there a way to set params.name() as an optional parameter (in the object) that can be a function or completely not exist (eg null )? 我的问题是:有没有办法将params.name()设置为可选参数(在对象中),可以是一个函数或完全不存在(例如null )? Something like saying: "when you instantiate the class: 有点像说:“当你实例化这个类时:

  • if you declare name: function (data) {} => then: params.name(data) is a function 如果你声明name: function (data) {} =>那么: params.name(data)是一个函数
  • if you skip name => it's anyway ok (no error is thrown)" 如果你跳过name =>无论如何都好(没有错误)

In ES6, You can use default function parameter option, like 在ES6中,您可以使用默认的函数参数选项

 var Class = function(params = { name: (x) => x }) { // I get the data somehow data='value'; params.name(data); } Class(); Class({name: x => console.log(x)}); 

Hope this will help! 希望这会有所帮助!

EDIT 编辑

Yes @brigo, you are right, sorry I overlooked that point. 是的@brigo,你是对的,对不起,我忽略了这一点。 We are actually looking for nested default rather. 我们实际上是在寻找嵌套的默认值。 A possible solution could be to destructure the object inside the function like 一个可能的解决方案可能是解析函数内部的对象

 var Class = function(params = {}) { // I get the data somehow data = 'value'; withDefaultParams = { name: (x) => x, ...params } withDefaultParams.name(data); } Class(); Class({ name: x => console.log('non-default') }); Class({ others: x => console.log('non-default') }); 

yes, you could do something like 是的,你可以做点什么

if (typeof param.name === 'function') {
    param.name(data);
} else {
    // dont do anything or do something different
}

If using jQuery is an option, you could use its extend method ( API docs ) like in the following example: 如果使用jQuery是一个选项,您可以使用其extend方法( API文档 ),如下例所示:

var Class = function(params) {

  var _defaults = {
      name: function(){},
      some: 'other stuff'
  };

  params = jQuery.extend(_defaults,params);

  // I get the data somehow
  data='value';
  params.name(data);
}

Class();
Class({foo:'bar'});
Class({name: console.log });

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

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