2015-01-07 15 views
2

如果你看一下這個代碼:JavaScript函數可以是另一個對象的類和實例嗎?

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

函數可以具有屬性以及原型。這些屬性可以直接在函數中訪問,但不能在函數的實例(類,構造函數等等)上訪問。 –

+1

JavaScript沒有類,所以應用來自其他編程語言的規則必然會造成混淆。 – JJJ

回答

3

對象既可以是一個類又可以是一個實例本身?

這不是這裏發生的事情。傳遞給函數的對象是一個實例。

該函數確實會修改您傳遞給它的實例以及該實例的類。

如果您創建了同一個類的兩個實例,並將其中一個傳遞給該函數,則另一個實例不會被修改,但它們共有的類會被修改。例如:

function MyClass() {} 

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

supportAggregate(a); 

現在既a.rebuildAggregatedb.rebuildAggregated存在,作爲被添加到類。 a.onModulesFoundAggregate存在,因爲它已添加到實例中,但b.onModulesFoundAggregate不存在。 (注意:這個例子實際上並不工作,因爲還有更多的事情要做,這個類必須有更多的屬性來處理這個函數,這個例子只是爲了顯示添加到原型的屬性之間的區別和實例。)

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的OBJ:

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 
相關問題