如何将原型和非原型对象文字与构造函数一起使用?
How to have both prototype and non-prototype object literals alongside constructor function?
以下是一个示例脚本,说明了我在理解 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() {},
// ...
};
function editor(){ /*...*/ }
Object.assign(editor, {
document(){//this is a new shortform for methods ;)
//whatever
}
//...
});
问题在于,无论何时将对象字面量分配给 editor,都会覆盖函数:
editor = {...};
由于无法构造具有属性的函数,我们需要先创建一个函数,然后分配我们的属性给它(就像我上面做的那样)。
以下是一个示例脚本,说明了我在理解 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() {},
// ...
};
function editor(){ /*...*/ }
Object.assign(editor, {
document(){//this is a new shortform for methods ;)
//whatever
}
//...
});
问题在于,无论何时将对象字面量分配给 editor,都会覆盖函数:
editor = {...};
由于无法构造具有属性的函数,我们需要先创建一个函数,然后分配我们的属性给它(就像我上面做的那样)。