我正在構建一個基於JavaScript的瀏覽器的Web應用程序。管理Javascript內存中變量的最佳方式是什麼
當我需要位於分隔文件的模塊,我不知道是哪三個方法是關於由JavaScript引擎所採取的內存最好:
理念1,分配變量的延伸方法在instanciation方法
function (ContactCollection , ItemCollection, ContactListView) {
var ContactExample = function (inst) {
// Wild examples of possible uses :
this.collections.contact.each(function(model) {
// do something with each model
});
this.collections.item.on('reset', this.resetItems, this);
this.$el.remove().append(this.view.render().el);
};
jQuery.extend(true, ContactExample.prototype, {
'collections': {
'contact': ContactCollection,
'item': ItemCollection
},
'view': ContactListView,
'$el': jQuery('#somediv'),
}, ContactExample);
return new ContactExample();
};
思想2,分配變量:
function (ContactCollection , ItemCollection, ContactListView) {
var ContactExample = function (inst) {
// Wild examples of possible uses :
inst.collections.contact.each(function(model) {
// do something with each model
});
inst.collections.item.on('reset', this.resetItems, this);
inst.$el.remove().append(this.view.render().el);
}
jQuery.extend(true, ContactExample.prototype, {
'$el': jQuery('#somediv')
}, ContactExample);
return new ContactExample({
'collections': {
'contact': ContactCollection,
'item': ItemCollection
},
'view': ContactListView
});
};
理念3,只需在代碼中使用它們,因爲它們在功能的範圍已經引用:
function (ContactCollection , ItemCollection, ContactListView) {
var ContactExample = function (inst) {
// Wild examples of possible uses :
ContactCollection.each(function(model) {
// do something with each model
});
ItemCollection.on('reset', this.resetItems, this);
this.$el.remove().append(ContactListView.render().el);
}
});
jQuery.extend(true, ContactExample.prototype, {
'$el': jQuery('#somediv')
}, ContactExample);
return new ContactExample();
});
什麼(爲什麼)是處理的最佳方式javascript內存引擎中的變量。
謝謝你的回答
可以去除backbone./require代碼,然後創造一個一般性的問題?這對其他人更有幫助,你會得到更多的迴應。 – 2013-03-12 10:25:22
我的建議是從頭開始使用可靠的骨幹教程,例如http://net.tutsplus.com/sessions/build-a-contacts-manager-using-backbone-js/或許還有助於完成JavaScript引用,如果你的背景是另一種技術,例如https://developer.mozilla.org/en-US/docs/JavaScript/Memory_Management – ricksuggs 2013-04-08 14:26:48
'jQuery.extend'不能像Backbone.View一樣使用。 extend'。您的問題目前無法解答,因爲它已被破壞。 – 2013-04-10 09:56:16