如何在声明期间设置定义为 属性 的函数的 属性
How to set a property of a function which is defined as a property, during declaration
here, this is a JavaScript and/or CodeMirror 问题中的 JSFiddle。
在下面的代码片段中,hint
函数被定义为 hintOptions
对象中的 属性。
是否可以设置该函数的 属性,而无需在代码块外定义它?
var editor = CodeMirror.fromTextArea(myTextarea, {
hintOptions: {
hint: function(cm, callback, options) {
return {
}
}
}
});
我尝试使用匿名函数,如:
var editor = CodeMirror.fromTextArea(myTextarea, {
hintOptions: {
hint: (function(cm, callback, options) {
return {
}
})({
async: true
})
}
});
但这似乎是语法错误,因为 JavaScript 根本不起作用。
作为 CodeMirror docs 提及:
hint: function
A hinting function, as specified above. It is possible to set the async
property on a hinting function to true, in which case it will be called with arguments (cm, callback, ?options)
检查 async
是否设置正确:
- 打开JSFiddle
- 点击'class code'
- 键入 Ctrl+Space
-
log
textarea 应该没有 undefined
对象初始化器中的 IIFE 用于创建具有 async
属性 的函数似乎有效:
let testObj = {
hintOptions: {
hint: (function () {
let hint = function(cm, callback, options) {
log(options);
return {
from: cm.getDoc().getCursor(),
to: cm.getaDoc().getCursor(),
list: ['foo', 'bar']
}
}
hint.async = true;
return hint
})()
}
};
console.log("hint.async: " + testObj.hintOptions.hint.async);
我按照 post 的说明设法在 fiddle 中获得了“[object Object]”,但不知道这是预期的结果。
here, this is a JavaScript and/or CodeMirror 问题中的 JSFiddle。
在下面的代码片段中,hint
函数被定义为 hintOptions
对象中的 属性。
是否可以设置该函数的 属性,而无需在代码块外定义它?
var editor = CodeMirror.fromTextArea(myTextarea, {
hintOptions: {
hint: function(cm, callback, options) {
return {
}
}
}
});
我尝试使用匿名函数,如:
var editor = CodeMirror.fromTextArea(myTextarea, {
hintOptions: {
hint: (function(cm, callback, options) {
return {
}
})({
async: true
})
}
});
但这似乎是语法错误,因为 JavaScript 根本不起作用。
作为 CodeMirror docs 提及:
hint: function
A hinting function, as specified above. It is possible to set the
async
property on a hinting function to true, in which case it will be called with arguments (cm, callback, ?options)
检查 async
是否设置正确:
- 打开JSFiddle
- 点击'class code'
- 键入 Ctrl+Space
-
log
textarea 应该没有undefined
对象初始化器中的 IIFE 用于创建具有 async
属性 的函数似乎有效:
let testObj = {
hintOptions: {
hint: (function () {
let hint = function(cm, callback, options) {
log(options);
return {
from: cm.getDoc().getCursor(),
to: cm.getaDoc().getCursor(),
list: ['foo', 'bar']
}
}
hint.async = true;
return hint
})()
}
};
console.log("hint.async: " + testObj.hintOptions.hint.async);
我按照 post 的说明设法在 fiddle 中获得了“[object Object]”,但不知道这是预期的结果。