lundi 2 février 2015

Javascript Reflection over many JS files

I'm trying to set up a testing library or rather improve on jsUnit - In Javascript the way I've always understood it and used it, you should be able to access any and all objects included via the window object


For example, in Test.js, I have 2 closures but they're labeled:



var objEntityTest = (function()
{
var objEntity = new Entity();

objEntity.addObserver(
{ update: function()
{
alert("Entity Test");
}
});

objEntity.setId(1);
})();

var objItemTest = (function()
{
var objItem = new Item();

objItem.addObserver(
{
update: function()
{
alert("Item Test");
}
});

objItem.setValue(new ItemValue(5, 3));
})();


The Test.js is loaded in the index.js and given a specific parameter, I can spark a round of testing. Except for some reason it's not being loaded.



<script>
$(document).ready(function()
{
alert(window.length);

for(obj in window)
{
var sName = getVariableName(obj);

if(sName.contains("Test"))
alert(sName);
});
</script>


Now note these are 2 different files. Except window.length returns 0 and the for loop used to return a seemingly infinite alert of 'objTest' but then I changed it to obj and then it never popped up so I added the alert for window.length.


A theory I have is that in Javascript, every file has its own "window" object - If that's true then I'll need to just go back to jsUnit and give it the tests to process.






Aucun commentaire:

Enregistrer un commentaire