我正在构建一个基于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