简体   繁体   English

关于 JavaScript 函数格式的问题

[英]Question about function format in JavaScript

I'm pretty new to JavaScript, and I'm working with JavaScript's datePicker library.我对 JavaScript 很陌生,我正在使用 JavaScript 的 datePicker 库。 I'm pretty much using the code out of the box, and the function that reads in the current date selected has me a bit confused.我几乎使用开箱即用的代码,读取当前所选日期的函数让我有点困惑。 Here's the code (which I've modified slightly to format the date - in order to string-match it to the dates of my dataset).这是代码(我稍微修改了它以格式化日期 - 为了将它与我的数据集的日期进行字符串匹配)。

const picker = datepicker(document.querySelector('#inlineDatepicker'), {

  // Event callbacks.
  onSelect: function(instance) {
  console.log(instance)
  var instanceSplit = instance.dateSelected.toString().split(" " ,4)
  var instanceClean = instanceSplit.toString().replace(/,/g, ' ')
    if (instanceClean === 'Mon Feb 11 2019') {
            console.log('datematch!')
            return 'blue'
        }       
      },
   });

I'm not familiar with the format of the onSelect: function(instance) {} function.我不熟悉onSelect: function(instance) {}函数的格式。 All the functions I've ever seen are written in this format: function someFunction(parameter) {} .我见过的所有函数都是用这种格式编写的: function someFunction(parameter) {}

Which leads me to my question: what is the actual function name of the onSelect: function(instance) {} function?这引出了我的问题: onSelect: function(instance) {}函数的实际函数名称是什么? For example, if I want to change the appearance of my data based on the date selected - ie return a stroke of 'blue' on my data points - what function name do I call?例如,如果我想根据所选日期更改数据的外观 - 即在我的数据点上返回“蓝色”笔划 - 我调用什么函数名称? For example, the code here attempts to pass in the returned stroke value in the above function, but it doesn't recognize onSelect as a function (says it's undefined):例如,这里的代码试图在上面的函数中传入返回的笔划值,但它不会将 onSelect 识别为函数(说它是未定义的):

var events = mapG.selectAll("circle")
    .data(allSFEvents)
    .enter().append("circle")
    .style("stroke", onSelect)

Depending on what you mean by "name", that function either doesn't have a name (it's anonymous ), which is the strict meaning according to the definition of the language;根据“名称”的含义,该函数要么没有名称(它是匿名的),这是根据语言定义的严格含义; or else the "name" in colloquial terms is the name of the property to which it's assigned.或者口语中的“名称”是指派给它的属性的名称。

The problem with the colloquial meaning, that the name is the property name, is that it can lead to false assumptions about the relationship between the anonymous function and the object where that property lives.名称就是属性名称的口语含义的问题在于,它可能导致对匿名函数与该属性所在的对象之间关系的错误假设。 There's no permanent relationship, in fact;事实上,没有永久的关系; the anonymous function is free of any implicit association with any "parent" object.匿名函数与任何“父”对象没有任何隐式关联。

In your case, it's unlikely that you'd ever want to directly call that function.在您的情况下,您不太可能想要直接调用该函数。 It's set up as an event handler, which means that the runtime or some framework will invoke your function at the appropriate time.它被设置为事件处理程序,这意味着运行时或某些框架将在适当的时间调用您的函数。 In general, if an object property refers to a function, you can call that function via the property:通常,如果对象属性引用函数,则可以通过属性调用该函数:

var someObject = {
  someFunction: function() { alert("Hi!"); }
};

someObject.someFunction(); // alerts "Hi!"

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

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