javascript函数可以是另一个对象的类和实例吗?
如果您查看此代码:javascript函数可以是另一个对象的类和实例吗?,javascript,mean.io,Javascript,Mean.io,如果您查看此代码: function supportAggregate(Meanio) { Meanio.prototype.aggregated = function(ext, group, callback) { // Aggregated Data already exists and is ready if (Meanio.Singleton.config.clean.aggregate === false){ r
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;
()
您可以看到为Meanio创建了两种类型的函数。另外,顺便说一句,您可以在这里看到它的实例化位置:
但我只是糊涂了。有时,Meanio函数的定义如下:
Meanio.prototype.myfunction = function() {}
Meanio.myfunction = function() {}
有时它们的定义如下:
Meanio.prototype.myfunction = function() {}
Meanio.myfunction = function() {}
我就是不明白;虽然我有一种感觉,依赖注入在某种程度上参与其中
这怎么可能?一个对象如何既是一个类又是其自身的一个实例
这段代码让我很困惑,如果有人能帮我解释一下,我会非常感激。我不是要你认真研究代码,但是如果你能给我一个大致的理解,那就太好了
提前谢谢
一个对象如何既是一个类又是其自身的一个实例
这不是这里发生的事。传递给函数的对象是一个实例
但是,该函数会修改传递给它的实例和该实例的类
如果创建同一类的两个实例,并将其中一个传递给函数,则不会修改另一个实例,但会修改它们公用的类。例如:
function MyClass() {}
var a = new MyClass();
var b = new MyClass();
supportAggregate(a);
现在,a.rebuildagregated
和b.rebuildagregated
都存在,因为它们被添加到类中。a.onmodulesfundaggregate
存在,因为它已添加到实例中,但b.onmodulesfundaggregate
不存在
(注意:这个例子实际上不起作用,因为还有更多的事情要做。这个类必须有更多的属性才能使用这个函数,这个例子只是为了显示添加到原型和实例中的属性之间的区别。)假设我有一个构造函数
// First I will define a constructor
function MyClass() {
this.classproperty = 1;
}
在Javascript中,构造函数也是一个对象实例。当我在构造函数中使用“this”关键字时,我告诉你我想在一个特殊对象中创建一个新属性,这个对象存在于所有名为prototype的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 obj的属性:
var instance = new MyClass();
alert(instance.newProperty); // undefined because the new instance will
// inherit only what is inside prototype obj
我必须将其添加到prototype对象中,以便新实例继承该属性
Myclass.prototype.newProperty = 2;
var instance = new Myclass();
alert(instance.newProperty) // alert 2
函数可以具有属性,也可以具有原型。属性可以直接在函数上访问,但不能在函数的实例(类、构造函数等)上访问。JavaScript没有类,因此应用其他编程语言中的规则必然会引起混淆。