"alex_gilboa" <al********@gmail.com> writes:
Oh, sorry for so unintelligible question. I've meant mock objects
implementation for javascript.
While there might be a framework for helping with this somewhere,
Javascript objects are so easily customizable that it's probably not
necessary.
If you need an object that returns three different names from a call
to getName, you could just write:
{index : 0,
getName : function() {
return ["Tim","Tom","Jack"][this.index++];
}}
Since Javascript objects doesn't have a type, they don't have to go
through hoops to look like a specific type of objects. If need be,
they can havve their prototype chain set to emulate a specific
constructor.
---
function mock(constructor) {
function Dummy(){};
Dummy.prototype=constructor.prototype;
return new Dummy();
}
---
If you want to change the behavior of existing objects, you can
just clone them and add properties to the clone:
---
function clone(object) {
function Dummy(){};
Dummy.prototype = object;
return new Dummy();
}
var o = {foo:42, bar: "I said 42!"};
var oc = clone(o);
oc.foo = 37;
alert([o.foo, o.bar, oc.foo, oc.bar]);
---
If this is enough for you, there is no need for mockery :)
/L
--
Lasse Reichstein Nielsen -
lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'