javascript函数可以是一个类和另一个对象的实例吗?

时间:2015-01-07 22:21:35

标签: 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){
          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() {}

我只是没有得到它;虽然我有一种感觉,依赖注入在某种程度上涉及。

这怎么可能?一个对象如何既是一个类又是一个自身的实例?

这段代码对我来说非常混乱,如果有人能为我阐明一下,我会非常感激。我并没有要求你大量研究这些代码,但如果你能给我一个大致的了解,那就太棒了。

提前致谢!

2 个答案:

答案 0 :(得分:3)

  

对象如何既是一个类又是一个自身的实例?

这不是在这里发生的事情。传递给函数的对象是一个实例。

但是,该函数会修改传递给它的实例以及该实例的类。

如果创建同一个类的两个实例,并将其中一个实例传递给该函数,则不会修改另一个实例,但会修改它们共有的类。例如:

function MyClass() {}

var a = new MyClass();
var b = new MyClass();

supportAggregate(a);

现在a.rebuildAggregatedb.rebuildAggregated都存在,因为它已添加到类中。 a.onModulesFoundAggregate存在,因为它已添加到实例中,但b.onModulesFoundAggregate不存在。

(注意:这个例子实际上没有用,因为还有更多的东西。这个类必须有更多的属性来处理这个函数,这个例子只是为了显示添加到该函数的属性之间的区别。原型和实例。)

答案 1 :(得分:0)

假设我有一个构造函数

// 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对象的属性:

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