Implemented constructor
parent
1ee270883e
commit
94835d290a
14
lib/class.js
14
lib/class.js
|
@ -31,6 +31,16 @@ var Class = function()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
var prop_copy = function( props, dest )
|
||||||
|
{
|
||||||
|
// copy each of the properties to the destination object
|
||||||
|
for ( property in props )
|
||||||
|
{
|
||||||
|
dest[ property ] = props[ property ];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mimics class inheritance
|
* Mimics class inheritance
|
||||||
*
|
*
|
||||||
|
@ -58,6 +68,10 @@ var extend = function()
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// copy the given properties into the new prototype
|
||||||
|
prop_copy( props, prototype );
|
||||||
|
|
||||||
|
// set up the new class
|
||||||
new_class.prototype = prototype;
|
new_class.prototype = prototype;
|
||||||
new_class.constructor = new_class;
|
new_class.constructor = new_class;
|
||||||
|
|
||||||
|
|
|
@ -32,17 +32,21 @@ assert.ok(
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
// these two variables are declared outside of the class to ensure that they
|
||||||
|
// will still be set even if the context of the constructor is wrong
|
||||||
|
var construct_count = 0,
|
||||||
|
construct_context = null;
|
||||||
|
|
||||||
// create a basic test class
|
// create a basic test class
|
||||||
var construct_count = 0;
|
|
||||||
var Foo = Class.extend(
|
var Foo = Class.extend(
|
||||||
{
|
{
|
||||||
__construct: function()
|
__construct: function()
|
||||||
{
|
{
|
||||||
construct_count++;
|
construct_count++;
|
||||||
|
construct_context = this;
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
assert.ok(
|
assert.ok(
|
||||||
( Foo instanceof Object ),
|
( Foo instanceof Object ),
|
||||||
"Extend method creates a new object"
|
"Extend method creates a new object"
|
||||||
|
@ -53,16 +57,29 @@ assert.ok(
|
||||||
"Created class contains extend method in prototype"
|
"Created class contains extend method in prototype"
|
||||||
);
|
);
|
||||||
|
|
||||||
|
assert.ok(
|
||||||
|
( Foo.prototype.__construct instanceof Function ),
|
||||||
|
"Provided properties should be copied to the new class prototype"
|
||||||
|
);
|
||||||
|
|
||||||
assert.equal(
|
assert.equal(
|
||||||
construct_count,
|
construct_count,
|
||||||
0,
|
0,
|
||||||
"Constructor should not be called before class is instantiated"
|
"Constructor should not be called before class is instantiated"
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
var obj = new Foo();
|
var obj = new Foo();
|
||||||
|
|
||||||
assert.equal(
|
assert.equal(
|
||||||
construct_count,
|
construct_count,
|
||||||
1,
|
1,
|
||||||
"Constructor should be invoked once the class is instantiated"
|
"Constructor should be invoked once the class is instantiated"
|
||||||
);
|
);
|
||||||
|
|
||||||
|
assert.equal(
|
||||||
|
obj,
|
||||||
|
construct_context,
|
||||||
|
"Constructor should be invoked within the context of the class instance"
|
||||||
|
);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue