Reputation: 1089
I am wondering which way is more efficient using jQuery data to bind data for object or using this kind of object. I am trying to create some kind of model for my app. Here is the object code
var PersonData = function () {
var that = {},
_name = 0,
_age = 0.0,
_domId = false;
that.data = initData();
//This is for initing data from options
function initOptions () {
return {
name: _name,
age: _age,
domId: _domId
}
}
that.setName = function (name) {
that.data.name = name;
}
that.getName = function () {
that.data.name;
}
// I forgot to add dom id, now there is id for binding
that.setDomElementId = function (id) {
that.data.domId = id;
}
//Add getters and setter
return that;
}
Thanks for your opinions
By the way is there good plugin for generating getters and setters in textmate for javascript
Upvotes: 3
Views: 1646
Reputation: 93714
jQuery's .data()
is for attaching data to DOM elements. If you're not touching the DOM, then don't touch .data()
.
Also, I don't think the kind of encapsulation you're going for is worthwhile in JavaScript (unless you require it to detect data read/writes). Especially if you're concerned about performance, directly accessing objects' properties is a much better option. JavaScript is a dynamic language, embrace it :)
Upvotes: 5