Object prototype change. Array like object
Test object prototype change for extended one. Test creating array like object. Setting length value doesn't change array.
by AntowaKartowa
JavaScript
window.mercanto = {
ids: {
uid: '1347134',
rid: '3741237846'
},
events: [
{ id: '001', name: 'aaaa' },
{ id: '002', name: 'bbbb' },
{ id: '003', name: 'aaaa' },
{ id: '004', name: 'bbbb' },
]
};
(function (mercantoObjName) {
var mercanto = getEmptyMercantoObject();
mercanto.ids = getIds( (window[mercantoObjName] || {}).ids, mercanto );
mercanto.events = getEvents( (window[mercantoObjName] || {}).events, mercanto );
window[mercantoObjName] = mercanto;
window[mercantoObjName].start();
function getEmptyMercantoObject () {
var mercantoProto = {
start: function () {
if (this.ids.uid && this.ids.rid) {
this.events.start();
}
},
push: function () {
this.events.push(arguments);
},
event: function () {
this.events.push(arguments);
},
login: function (id) {
this.ids.login(id);
},
logout: function () {
this.ids.logout();
},
pushEvent: function (event) {
var uid = event.name === 'logout' && this.ids.ouid || this.ids.uid;
console.log('Pushing "' + event.name + '" event #' + event.id + ' for user #' + uid + ' and customer #' + this.ids.rid);
}
};
return Object.create(mercantoProto);
}
function getIds (ids, parentObj) {
var idsProto = {
_super: parentObj,
start: function () {
if (this.uid !== null) {
this.login(this.uid);
}
},
login: function (id) {
this.uid = id;
var event = {
id: Math.round(Math.random() * 1000),
name: 'login',
createdAt: (new Date()).getTime()
};
this._super.pushEvent(event);
},
logout: function () {
var event = {
id: Math.round(Math.random() * 1000),
name: 'logout',
createdAt: (new Date()).getTime()
};
this._super.pushEvent(event);
this.ouid = this.uid;
...