2016-05-12 85 views
2

例如,假設我真的很餓,所以我只是繼續製作煎餅!在Javascript中,有沒有一種方法可以統計我創建了多少個創建的對象?

var Buttermilk = new Pancake("Buttermilk", "Delicious"); 
var ChocolateChip = new Pancake("Chocolate Chip", "Amazing"); 
var BlueBerry = new Pancake("Blue Berry", "The Best"); 
var SnozBerry = new Pancake("Snoz Berry", "What's a Snoz Berry?"); 

我該如何計算我手動製作的煎餅數量?是否有代碼說「有這麼多變量是煎餅品種」?

編輯:

謝謝你的答案!我專門尋找一種簡單的方法來快速計算我用少量代碼創建對象的次數。這就是我得到的,謝謝!

+0

你可以添加你的煎餅班嗎? –

+1

相當肯定你必須手動完成它... – James

+0

可能重複的[如何計算對象的實例?](http://stackoverflow.com/questions/12378909/how-can-i-count對象的實例) –

回答

2

你可以在JavaScript類中具有靜態屬性。您可以把他們藏在封這樣:

var Pancake = (function() { 
    var instances = 0; 
    return function(a, b) { 
     this.a = a; 
     this.b = b; 
     instances++; 

     Pancake.prototype.instances = function() { // equivalent of a static method 
      return instances; 
     } 
    }; 
}()); 

或把它們放到對象原型:

var pancake = function(a, b) { 
    this.a = a; 
    this.b = b; 
    pancake.prototype.count = pancake.prototype.count ? pancake.prototype.count + 1 : 1; // equivalent of a static property 
} 

你也可以「覆蓋」的構造,通過實施某種「繼承」,如在此琴:

var Pancake = function(a, b) { 
 
\t this.a = a; 
 
    this.b = b; 
 
}; 
 

 
var before = Pancake.prototype; 
 
var Pancake = function() { 
 
\t console.log("overriden"); 
 
\t Pancake.prototype.instances = Pancake.prototype.instances ? Pancake.prototype.instances + 1 : 1; // here you should restore the whole prototype 
 
    return before.constructor.apply(this, arguments); 
 
}; 
 

 

 
var a = new Pancake("a", "b"); 
 
document.write(Pancake.prototype.instances + "<br />"); 
 
var b = new Pancake("c", "d"); 
 
document.write(Pancake.prototype.instances + "<br />"); 
 

 
document.write(JSON.stringify(a) + "<br />"); 
 
document.write(JSON.stringify(b) + "<br />");

+0

非常感謝!哇,我不相信我沒那麼做!我完全忘記了它們是功能而不僅僅是物體! – Raymond

+1

@Raymond認爲你不想手動創建它!那就是你的問題所說的!? –

+0

@NidhinDavid啊,手動我的意思是每次我創建一個我喜歡的對象的新實例,添加到一些東西。所以在每一個新的煎餅之後,我會給1加1或者什麼的。我沒有意識到我只需要將一小部分代碼(我簡直就是把count ++)放入函數中,每次創建一個新實例時都會添加它。 – Raymond

1

使用計數器變量煎餅函數內部.. :)

var count = 0; 
function Pancake(){ 
// Cook pancakes 
count += 1; 
} 

console.log('Total pancakes' + count); 
+3

'沒有手動做它' –

+0

謝謝!我明白了,非常簡單的答案!謝謝! – Raymond

+0

歡迎您! – Vinay

2

您可以保留一個計數器,這將在構造函數中得到增量,這裏是一個很好的解決方案

How can I count the instances of an object?

+1

啊,有幫助!我在編碼方面並不是最偉大的,所以具體和例子讓我感到困惑(爲什麼我以煎餅爲例)謝謝你! – Raymond

0

我知道你已經接受了一個答案,但是這花了我一段時間!從你的問題,我想你可能不想改變煎餅班。所以這是爲了避免這種情況。

該函數將搜索您指定的對象中的所有對象並計算您的類型的所有實例。

// Usage: 
    searchObjectForType(window, Pancake); // returns a number. 

    function searchObjectForType(obj,type){ 
     // Keep track of objects in stack so we don't overflow by searching into the same objects; 
     var stackObjs = []; 

     function recursiveProbe(obj){ 
      var foundCount = 0; 
      var objType; 
      // Some types will throw (iframes/nulls/..) 
      try{ 
      objType = obj.toString(); 
      } 
      catch(err){ 
      return 0; 
      } 
      // Skip document/previous objects as we know they won't have any. 
      if(typeof obj === "string" || stackObjs.indexOf(objType)>=0 || obj===document){ 
      return 0; 
      } 
      else{ 
      stackObjs.push(objType); 
      } 

      for (var i in obj){ 
      var prop = obj[i]; 
      if(prop instanceof type){ 
       foundCount++; 
      } 
      else{ 
       foundCount += recursiveProbe(prop); 
      } 
      } 
      // Remove this type from stackObjs so we can search future types. 
      stackObjs.splice(stackObjs.indexOf(obj.toString()),1); 
      return foundCount; 
     } 

     return recursiveProbe(obj); 
    }    

我敢肯定,有些情況下,這失敗了,所以反饋讚賞!

相關問題