以下是一个示例脚本,说明了我理解JavaScript的文字和原型行为的问题。有没有办法将下面列出的辅助函数(即editor.document(),editor.fragment()等)放在对象文字中,就像你看到我使用原型方法(即editor.prototype.inject() ,editor.prototype.selection()等)。我无法找到一种方法将辅助函数放在对象文字中而不会覆盖构造函数。
我知道我可以将整个脚本封装在一个函数中,但我的目标是不将辅助函数设置为私有,我只是希望它们是命名空间的,但仍然可以在editor()构造函数所在的相同范围内访问。
我唯一可以假设的是这不是可能的,或者我对JavaScript的对象文字有些误解(非原型文字等同于自动创建的实例)字面意思?)...
/**
* Editor's constructor.
*/
editor = function(config) {
// Setup object
this.config = config;
// ...
// Chainable
return this;
};
/**
* Helper functions that can be called directly without instantiating
* the "namespace" (i.e. editor) constructor. For readability, is it
* possible to put these in an object literal outside of prototype?
*/
editor.document = function() {};
editor.fragment = function() {};
editor.isHtml = function() {};
editor.isTag = function() {};
editor.toNodes = function() {};
/**
* Functions requiring an instance of editor. They can be placed in
* a pretty object literal that's easy to read... But what about the
* helper methods above that I just want as part of the editor
* namespace but not part of the instance? Why can't those be in an
* object literal? Am I missing something?
*/
editor.prototype = {
// ...
config : {},
inject : function() {},
selection : function() {},
// ...
};
答案 0 :(得分:2)
Field uniqueAttributeName = MySchema.PersistentIOU.class.getDeclaredField("fieldname");
CriteriaExpression uniqueAttributeEXpression = Builder.equal(uniqueAttributeName, "valueToSearch");
QueryCriteria customCriteria = new QueryCriteria.VaultCustomQueryCriteria(uniqueAttributeEXpression);
result = rpcOps.vaultQueryByCriteria(customCriteria, MyState.class).getStates();
问题在于,无论何时将对象文字指定给编辑器,都将覆盖该函数:
{{1}}
由于无法构造具有属性的函数,我们需要先创建一个函数,然后将我们的属性赋值给它(就像我上面所做的那样)。