ENUMERABLE.JSENUMERABLE.JSENUMERABLE.JSENUMERABLE.JSENUMERABLE.JSENUMERABLE.JS
ES5 IS NOT OPERATING ON NATIVE OBJECTS
WE CAN'T CALL ES5 METHODS IN OBJECT CONTEXT
alert([].map.call({head:15,title:30},function(s){return s+'px'}).join(', '))
SOMETIMES WE CAN, HOLY GRAIL..
var o={foo:"foo"}
Array.push(o, 'foo');
Array.push(o, bar);
alert( o.toSource() );//prints:({foo:"foo", length:1, 1:"bar"})
SOMETIMES WE MISUNDERSTOOD ARGUMENTATIONS
var r=
[
{id:0,title:'poem'}
].
filter(function(book){
alert(book.id+" : "+book.title+" : "+this.author);
return book.id==4
},
{author:'Shakespeare'});
WHERE HAS MY AUTHORS DISAPPEARED?
r.forEach(function(book){
alert(book.id+" : "+this.author);//prints 0+" : "+undefned
})
"HOW ABOUT EMULATING AN ARRAY INSIDE AN OBJECT?"
@Angus Croll
WE WILL HAVE TROUBLE WITH LENGTH
LET'S ITERATE THROUGH OBJECT TO MIMIC INDEX
var index = 0;
for(var prop in this){
if(!this.__lookupGetter__(prop) &&
this.constructor!=Array){
(function(thisIndex, thisProp){
Object.defineProperty(obj, thisIndex, {
get: function(){return obj[thisProp]}
, enumerable: true
, configurable: true
, writeable: true
});
})(index, prop)
index++;
}
}
UPDATE OBJECT
Object.defineProperty(obj, 'length', {
get: function(){
return obj.update();
}
set: function(){
return obj.update();
}
, configurable : false
, enumerable : false
, writeable : true
});
obj.length;
CCCHHHEEECCCKKK TTTHHHEEE SSSOOOUUURRRCCCEEE DDDUUUDDDEEE!!!
WORKS?
var myObj={jozsi:2,tamas:5};
//wrap up our object extensional expressions to a method;
extendObj_as_array(myObj);
alert('myObj is now an emulated kind of array '+myObj.length);
//adding new, uninitialized property also as array indices;
myObj.aron=0;
NO...
//it is not possible to query the newly added index to myObj,
//because index is actually uninitialized until the length is queried;
alert('newly added index is undefined: '+myObj[2]);
//length is queried, indices are being updated!
alert('indices are updated: '+myObj.length);
//Now it is possible to query the newly added index to myObj;
alert('newly added index is now defined: '+myObj[2]);
//array functionality works on objects;
alert('mapping values as pixels: '+
[].map.call(myObj,function(s){return s+'px'}).join(', '));
//prints "2px, 5px, 0px"
OBJECT IS KEY-VALUE PAIR RETAINING THE ORDER
SO ENCAPSULATE DIFFERENCES!
var enum=enumerable();
var enum=enumerable([]);
var enum=enumerable({});
var enum=enumerable(new enumerable);
LET US WORK!
//note! vacuum.db.tables[table] is a valid JavaScript Object.
enumerable(vacuum.db.tables[table]).
sort({againstVal:true,func:function(a,b){return a.frequency<b.frequency}}).
.forEach(function(i,val){
if(it.isTypeOf(val,[Object]).
ANDNOT({macro:'$this.id=='+self.defaultRecord}).true())
{
self.record(table,val.id);
}
return true;
});
FEATURES & PRINCIPLES
functional break support
Undo-redo transactions
Recursion support
returning interface consistency
Rock solid ordering management
FEATURES BASED ON ORDERING
Comparison between different types
Order based comparisons
fromItem/toItem support(bubbling)
BIBLIOGRAPY
Angus Croll
HTMLpiknik
Problems with extending regular Objects
enumeration order guarantee

Enumerable.js

  • 1.
  • 2.
    ES5 IS NOTOPERATING ON NATIVE OBJECTS
  • 3.
    WE CAN'T CALLES5 METHODS IN OBJECT CONTEXT alert([].map.call({head:15,title:30},function(s){return s+'px'}).join(', '))
  • 4.
    SOMETIMES WE CAN,HOLY GRAIL.. var o={foo:"foo"} Array.push(o, 'foo'); Array.push(o, bar); alert( o.toSource() );//prints:({foo:"foo", length:1, 1:"bar"})
  • 5.
    SOMETIMES WE MISUNDERSTOODARGUMENTATIONS var r= [ {id:0,title:'poem'} ]. filter(function(book){ alert(book.id+" : "+book.title+" : "+this.author); return book.id==4 }, {author:'Shakespeare'});
  • 6.
    WHERE HAS MYAUTHORS DISAPPEARED? r.forEach(function(book){ alert(book.id+" : "+this.author);//prints 0+" : "+undefned })
  • 7.
    "HOW ABOUT EMULATINGAN ARRAY INSIDE AN OBJECT?" @Angus Croll
  • 8.
    WE WILL HAVETROUBLE WITH LENGTH
  • 9.
    LET'S ITERATE THROUGHOBJECT TO MIMIC INDEX var index = 0; for(var prop in this){ if(!this.__lookupGetter__(prop) && this.constructor!=Array){ (function(thisIndex, thisProp){ Object.defineProperty(obj, thisIndex, { get: function(){return obj[thisProp]} , enumerable: true , configurable: true , writeable: true }); })(index, prop) index++; } }
  • 10.
    UPDATE OBJECT Object.defineProperty(obj, 'length',{ get: function(){ return obj.update(); } set: function(){ return obj.update(); } , configurable : false , enumerable : false , writeable : true }); obj.length;
  • 11.
  • 12.
    WORKS? var myObj={jozsi:2,tamas:5}; //wrap upour object extensional expressions to a method; extendObj_as_array(myObj); alert('myObj is now an emulated kind of array '+myObj.length); //adding new, uninitialized property also as array indices; myObj.aron=0;
  • 13.
    NO... //it is notpossible to query the newly added index to myObj, //because index is actually uninitialized until the length is queried; alert('newly added index is undefined: '+myObj[2]); //length is queried, indices are being updated! alert('indices are updated: '+myObj.length); //Now it is possible to query the newly added index to myObj; alert('newly added index is now defined: '+myObj[2]); //array functionality works on objects; alert('mapping values as pixels: '+ [].map.call(myObj,function(s){return s+'px'}).join(', ')); //prints "2px, 5px, 0px"
  • 14.
    OBJECT IS KEY-VALUEPAIR RETAINING THE ORDER
  • 15.
    SO ENCAPSULATE DIFFERENCES! varenum=enumerable(); var enum=enumerable([]); var enum=enumerable({}); var enum=enumerable(new enumerable);
  • 16.
    LET US WORK! //note!vacuum.db.tables[table] is a valid JavaScript Object. enumerable(vacuum.db.tables[table]). sort({againstVal:true,func:function(a,b){return a.frequency<b.frequency}}). .forEach(function(i,val){ if(it.isTypeOf(val,[Object]). ANDNOT({macro:'$this.id=='+self.defaultRecord}).true()) { self.record(table,val.id); } return true; });
  • 17.
    FEATURES & PRINCIPLES functionalbreak support Undo-redo transactions Recursion support returning interface consistency Rock solid ordering management
  • 18.
    FEATURES BASED ONORDERING Comparison between different types Order based comparisons fromItem/toItem support(bubbling)
  • 19.
    BIBLIOGRAPY Angus Croll HTMLpiknik Problems withextending regular Objects enumeration order guarantee