我正在使用JavaScript,並且想知道構造函數內部的對象數量。如何計算構造函數中的對象的數量javascript
我有這樣的:
var EventData = function(ID, Name, StartDate, StartTime, EndDate, EndTime, Location, Notes){
this.type = "event";
this.id = ID;
this.name = Name;
this.startDate = StartDate;
this.startTime = StartTime;
this.endDate = EndDate;
this.endTime = EndTime;
this.location = Location;
this.notes = Notes;
};
EventData.prototype.count = function(){
return //some code;
};
,我想打電話給這樣的事情:
var Start = function(){
var thisEventData = new EventData(1,"Bill", 1,1,1,1, "Home", "N/A");
console.log(thisEventData.count());
};
凡thisEventData.count()將返回10.
「構造的內部對象的數量」不賺很多的意義。你可以用Object.keys(obj).length'來計算對象的枚舉屬性(不管屬性來自何處)。 – Pointy
恩,你的意思是返回9,對吧? – adeneo
您不必原型計數功能。很簡單,因爲EventData.length – manuerumx