Sunday, 15 April 2012

actionscript 3 - How would Object.defineProperty be in AS3? -



actionscript 3 - How would Object.defineProperty be in AS3? -

i'm architect strong javascript background, did .net , java in past.

however, wanted set hand on actionscript3, promised related javascript.

as startup project took on myself seek port actionscript3 1 of favorite assertion utils - should.js - makes test codes pleasant read.

updated: 2013-02-19

i saw confuse abstract speaking, replaced of post concrete question in mind. here's total picture:

consider next javascript code:

object.defineproperty(object.prototype, 'should' , { set: function(){} , get: function(){ homecoming new assertion(object(this).valueof()); } , configurable: true , enumerable : false } );

that part of implementation of javascript module should. other part definition of class assertion, constructed value, , implements wide , nice set of assertion methods, against value. methods similar

var o = assertion(actualvalue) o.equals(expectedvalue1) o.morethan(expectedvalue2) o.contains(expectedvalue3)

and aliases maintain english language grammer

var o = assertion(actualvalue) o.equal(expectedvalue1) o.contain(expectedvalue3)

and aliases lazy sharpshooters, like

o.eql(expectedvalue) o.gt(expectedvalue) //greater o.gte(...) //greater or equal //and on...

and connectors homecoming this, (which instance of assertion constructed test value)

o.be o.and

what give you?

a test code looks this: var person = getperson(); should.exist(person); //that's static call, , that's easy //but these fellow member calls: person.should.have("name","age","address","friends"); person.name.should.equal("john"); person.age .should .be.number() .and.be.between(20,30); person.address .should .be.string().and .startwith("\d").and .endwith(" st.") //or .and.match(/^[0-9]{1,9}\s+[a-z][a-z0-9 ]* st\.$/); person.friends .should .be.array().and .be.between(3,5).and .containonlytype(string);

isn't wonderful? it's plain english!

you argue aesthetics of indentation, set and, , if @ necessary, besides - can read or write it: 1 time took 'should' attribute exists on every object not spoil map iterations - can go on chaining whatever have claim regarding value started from.

it have more nifty iteration tools, reflection utilities, augmented test functions relevant object model, , on , forth, lets on first step :)

but that, need every object in scheme feature non-enumerable smart property called should in it's getter function returns assertion object constructed this tested value.

(you ain't seen nil yet - wait see beautiful rejection messages gives! yummie!! yea - happily sacrifice alternative phone call attribute "should"... , happily give intelisense - @ to the lowest degree long it's plain english)

so, in comments, bfavaretto gave first step - know how prevent enumeration of attribute - great & thanks!!

now, can create getter-attribute who's function can access this?

when i'm done i'm going set in public repo licensed under mit, of have fun :)

help anybody?

you illustration 90% right - define actionscript, not javascript!

you can still define prototypes in as3 , still work prototypes in as2. difference in as3 compiler. avm2 reason not cast prototypes native classes (although didn't test custom classes).

the prototype trick: cast class object.

eg: if create:

array.prototype.random = function():void{}

then create object:

var myprotoarray:array = new array;

2 things happen:

myprotoarray.random() //error - fail, avm2 did not map prototype array

but

object(myprotoarray).random() //works

random() cast object class, mapped array - have no thought why!

hope helps, cheers.

actionscript-3 unit-testing porting assertions should.js

No comments:

Post a Comment