javascript 函数可以是 class 和另一个对象的实例吗?
Can a javascript function be a class and an instance of another object?
如果您查看这段代码:
function supportAggregate(Meanio) {
Meanio.prototype.aggregated = function(ext, group, callback) {
// Aggregated Data already exists and is ready
if (Meanio.Singleton.config.clean.aggregate === false){
return callback('');
}
if (aggregated[group][ext].data) return callback(aggregated[group][ext].data);
// No aggregated data exists so we will build it
sortAggregateAssetsByWeight();
// Returning rebuild data. All from memory so no callback required
callback(aggregated[group][ext].data);
};
Meanio.prototype.aggregatedsrc = function(ext, group, callback) {
// Aggregated Data already exists and is ready
if (Meanio.Singleton.config.clean.aggregate !== false){
if(ext==='js'){
if(group==='header'){
return callback(['/modules/aggregated.js?group=header']);
}else{
return callback(['/modules/aggregated.js']);
}
}else if(ext==='css' && group==='header'){
return callback(['/modules/aggregated.css']);
}
return callback([]);
}
if (aggregated[group][ext].src) return callback(aggregated[group][ext].src);
// No aggregated data exists so we will build it
sortAggregateAssetsByWeight();
// Returning rebuild data. All from memory so no callback required
callback(aggregated[group][ext].src);
};
// Allows rebuilding aggregated data
Meanio.prototype.rebuildAggregated = function() {
sortAggregateAssetsByWeight();
};
Meanio.prototype.Module.prototype.aggregateAsset = function(type, asset, options) {
options = options || {};
if (!options.inline && !options.absolute && !options.url) {
asset = path.join(Meanio.modules[this.name].source, this.name, 'public/assets', type, asset);
}
Meanio.aggregate(type, asset, options, Meanio.Singleton.config.clean);
};
Meanio.onModulesFoundAggregate = function(ext, options) {
var config = Meanio.Singleton.config.clean;
var aggregator = new Aggregator(options, false, config);
for (var name in Meanio.modules) {
aggregator.readFiles(ext, path.join(process.cwd(), Meanio.modules[name].source, name.toLowerCase(), 'public'));
}
};
Meanio.aggregate = function(ext, asset, options, config) {
var aggregator;
options = options || {};
if (!asset) {
return;
}
aggregator = new Aggregator(options, true, config);
if (options.inline) return aggregator.addInlineCode(ext, asset);
else if (options.url) return aggregator.getRemoteCode(ext, asset);
else if (options.singlefile) return aggregator.processDirOfFile(ext, asset);
else return aggregator.readFile(ext, path.join(process.cwd(), asset));
};
Meanio.prototype.aggregate = Meanio.aggregate;
}
module.exports = supportAggregate;
(https://github.com/linnovate/meanio/blob/master/lib/aggregation.js#L213)
您可以看到Meanio 创建了两种类型的函数。另外,顺便说一句,您可以在此处查看它的实例化位置:https://github.com/linnovate/meanio/blob/master/lib/mean.js#L114
但我只是很困惑。有时,Meanio 函数定义如下:
Meanio.prototype.myfunction = function() {}
有时它们是这样定义的:
Meanio.myfunction = function() {}
我只是不明白;尽管我有一种感觉,在某种程度上涉及依赖注入。
怎么会这样?对象如何既是 class 又是其自身的实例?
这段代码让我很困惑,如果有人能帮我解释一下,我将不胜感激。我不是要你深入研究代码,但如果你能给我一个大概的理解,那就太好了。
提前致谢!
How can an object be both a class and an instance of itself?
这不是这里发生的事情。传递给函数的对象是一个实例。
然而,该函数确实修改了您传递给它的实例,以及该实例的 class。
如果您创建两个相同 class 的实例,并将其中一个传递给函数,则不会修改另一个实例,但会修改它们共有的 class。示例:
function MyClass() {}
var a = new MyClass();
var b = new MyClass();
supportAggregate(a);
现在 a.rebuildAggregated
和 b.rebuildAggregated
都存在,因为它们已添加到 class。 a.onModulesFoundAggregate
存在是因为它已添加到实例中,但 b.onModulesFoundAggregate
不存在。
(注意:这个例子实际上不会工作,因为还有更多的事情要做。class 必须有更多的属性才能使用那个函数,这个例子只是为了展示两者之间的区别添加到原型和实例的属性。)
假设我有一个构造函数
// First I will define a constructor
function MyClass() {
this.classproperty = 1;
}
在Javascript中构造函数也是一个对象实例。当我在构造函数中使用 "this" 关键字时,我告诉我想在所有 javascript 对象中存在的特殊对象中创建一个新的 属性 称为原型。
// Then I add a new property without using prototype obj
MyClass.newProperty = 2;
alert(MyClass.classproperty); // alert 1
alert(MyClass.newProperty); // alert 2
// It will work because I'm using the MyClass main Object
当您从 Myclass Obj 创建新实例时。新创建的对象将继承父对象(用于实例化的对象)的原型对象,但不会直接添加到 MyClass 对象的属性:
var instance = new MyClass();
alert(instance.newProperty); // undefined because the new instance will
// inherit only what is inside prototype obj
我必须将它添加到原型对象,以便新实例继承 属性;
Myclass.prototype.newProperty = 2;
var instance = new Myclass();
alert(instance.newProperty) // alert 2
如果您查看这段代码:
function supportAggregate(Meanio) {
Meanio.prototype.aggregated = function(ext, group, callback) {
// Aggregated Data already exists and is ready
if (Meanio.Singleton.config.clean.aggregate === false){
return callback('');
}
if (aggregated[group][ext].data) return callback(aggregated[group][ext].data);
// No aggregated data exists so we will build it
sortAggregateAssetsByWeight();
// Returning rebuild data. All from memory so no callback required
callback(aggregated[group][ext].data);
};
Meanio.prototype.aggregatedsrc = function(ext, group, callback) {
// Aggregated Data already exists and is ready
if (Meanio.Singleton.config.clean.aggregate !== false){
if(ext==='js'){
if(group==='header'){
return callback(['/modules/aggregated.js?group=header']);
}else{
return callback(['/modules/aggregated.js']);
}
}else if(ext==='css' && group==='header'){
return callback(['/modules/aggregated.css']);
}
return callback([]);
}
if (aggregated[group][ext].src) return callback(aggregated[group][ext].src);
// No aggregated data exists so we will build it
sortAggregateAssetsByWeight();
// Returning rebuild data. All from memory so no callback required
callback(aggregated[group][ext].src);
};
// Allows rebuilding aggregated data
Meanio.prototype.rebuildAggregated = function() {
sortAggregateAssetsByWeight();
};
Meanio.prototype.Module.prototype.aggregateAsset = function(type, asset, options) {
options = options || {};
if (!options.inline && !options.absolute && !options.url) {
asset = path.join(Meanio.modules[this.name].source, this.name, 'public/assets', type, asset);
}
Meanio.aggregate(type, asset, options, Meanio.Singleton.config.clean);
};
Meanio.onModulesFoundAggregate = function(ext, options) {
var config = Meanio.Singleton.config.clean;
var aggregator = new Aggregator(options, false, config);
for (var name in Meanio.modules) {
aggregator.readFiles(ext, path.join(process.cwd(), Meanio.modules[name].source, name.toLowerCase(), 'public'));
}
};
Meanio.aggregate = function(ext, asset, options, config) {
var aggregator;
options = options || {};
if (!asset) {
return;
}
aggregator = new Aggregator(options, true, config);
if (options.inline) return aggregator.addInlineCode(ext, asset);
else if (options.url) return aggregator.getRemoteCode(ext, asset);
else if (options.singlefile) return aggregator.processDirOfFile(ext, asset);
else return aggregator.readFile(ext, path.join(process.cwd(), asset));
};
Meanio.prototype.aggregate = Meanio.aggregate;
}
module.exports = supportAggregate;
(https://github.com/linnovate/meanio/blob/master/lib/aggregation.js#L213)
您可以看到Meanio 创建了两种类型的函数。另外,顺便说一句,您可以在此处查看它的实例化位置:https://github.com/linnovate/meanio/blob/master/lib/mean.js#L114
但我只是很困惑。有时,Meanio 函数定义如下:
Meanio.prototype.myfunction = function() {}
有时它们是这样定义的:
Meanio.myfunction = function() {}
我只是不明白;尽管我有一种感觉,在某种程度上涉及依赖注入。
怎么会这样?对象如何既是 class 又是其自身的实例?
这段代码让我很困惑,如果有人能帮我解释一下,我将不胜感激。我不是要你深入研究代码,但如果你能给我一个大概的理解,那就太好了。
提前致谢!
How can an object be both a class and an instance of itself?
这不是这里发生的事情。传递给函数的对象是一个实例。
然而,该函数确实修改了您传递给它的实例,以及该实例的 class。
如果您创建两个相同 class 的实例,并将其中一个传递给函数,则不会修改另一个实例,但会修改它们共有的 class。示例:
function MyClass() {}
var a = new MyClass();
var b = new MyClass();
supportAggregate(a);
现在 a.rebuildAggregated
和 b.rebuildAggregated
都存在,因为它们已添加到 class。 a.onModulesFoundAggregate
存在是因为它已添加到实例中,但 b.onModulesFoundAggregate
不存在。
(注意:这个例子实际上不会工作,因为还有更多的事情要做。class 必须有更多的属性才能使用那个函数,这个例子只是为了展示两者之间的区别添加到原型和实例的属性。)
假设我有一个构造函数
// First I will define a constructor
function MyClass() {
this.classproperty = 1;
}
在Javascript中构造函数也是一个对象实例。当我在构造函数中使用 "this" 关键字时,我告诉我想在所有 javascript 对象中存在的特殊对象中创建一个新的 属性 称为原型。
// Then I add a new property without using prototype obj
MyClass.newProperty = 2;
alert(MyClass.classproperty); // alert 1
alert(MyClass.newProperty); // alert 2
// It will work because I'm using the MyClass main Object
当您从 Myclass Obj 创建新实例时。新创建的对象将继承父对象(用于实例化的对象)的原型对象,但不会直接添加到 MyClass 对象的属性:
var instance = new MyClass();
alert(instance.newProperty); // undefined because the new instance will
// inherit only what is inside prototype obj
我必须将它添加到原型对象,以便新实例继承 属性;
Myclass.prototype.newProperty = 2;
var instance = new Myclass();
alert(instance.newProperty) // alert 2