G
ged
Hi,
i am a oo (c#) programmer, and have not used javascript for a while and
i cant work out how javascript manages its references. Object
References work for simple stuff, but once i have an object collection
and stanrd using it it starts to fall apart.
Clearly there is something about javascript's usage of passing "By ref"
that i am not getting. i have had a look on the web and found some
examples, but i cant see why my code does not work. Grrrrh. I really
need to know why because i am writng a large app in javascript and
Ajax.
I program in c# mainly and i am applying soem OO principles to my
javascript but i am goign nust with how its works sometimes.
Also on a side not i dont get why i code javaScript this way to make it
work?
var personEntity = new PersonEntity("ged", 35);
document.write(personEntity.Name;
function PersonEntity(name, age)
{
var Name = name;
var Age - age;
}
it has to be instead:
function PersonEntity(name, age)
{
this.Name = name;
this.Age = age;
}
i have a sneaking suspicion that the above example also has something
to do with my misunderstanding of the javascript reference pointers
work.
Code is below as a complete test. You just have to save it and load
into your browser and it will run telling you where it errored. I
really want an answer on this and am trying to make it easy for someone
to tell me why is DOES NOT WOKR .
thansk in advance
Ged
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Reference Test</title>
<script language="JavaScript">
// Constants maps to static UI element ids
var APP_ICON_XPLORER_ID = "XplorerAppIcon";
var APP_ICON_GIMP_ID = "GimpAppIcon";
var APP_PANEL_XPLORER_ID = "XplorerAppPanel";
var APP_PANEL_GIMP_ID = "GimpAppPanel";
var g_appController = new AppController();
var g_appModel = new AppModel();
function AppInit()
{
// create app objects to map Dom Elements to other aspects
var xplorerApp = new AppItem('Xplorer', APP_ICON_XPLORER_ID,
APP_PANEL_XPLORER_ID);
g_appModel.Add(xplorerApp);
var gimpApp = new AppItem('Gimp', APP_ICON_GIMP_ID,
APP_PANEL_GIMP_ID)
g_appModel.Add(gimpApp);
// DO test of item being clicked
g_appController.AppItemLoadCommand(APP_ICON_XPLORER_ID);
}
function AppController()
{
// App Icon clicked
this.AppItemLoadCommand = function(id)
{
// resolve item clicked
var appItem = g_appModel.GetAppItemByIconElementId(id);
document.write("appItem.appIconElementId : " +
appItem.appIconElementId + "<BR />");
// Set Model
g_appModel.SetCurrentItem(appItem);
// Set View
var appView = AppView();
appView.SetFocus(g_appModel.appList, appItem);
}
}// JScript File
function AppModel()
{
this.currentItem = null;
this.appList = new Array();
this.Add = function(appItem)
{
this.appList[this.appList.length] = appItem;
};
this.GetAppItemByIconElementId = function(id)
{
for(var x=0; x < this.appList.length; x++)
{
if(this.appList[x].appIconElementId == id)
{
return this.appList[x];
}
return null;
}
}
this.GetAppItemByPanelElementId = function(id)
{
for(var x=0; x < this.appList.length; x++)
{
if(this.appList[x].appPanelElementId == id)
{
return this.appList[x];
}
return null;
}
}
this.SetCurrentItem = function(appitem)
{
document.write("Testing object passed by reference now <BR
/>");
// !! This fails to have the object passed in - whats gives
?//
// Gtes appItem is Not defines in javascript console //
document.write("appItem.appIconElementId : " +
appItem.appIconElementId);
appItem.isCurrent = true;
}
this.GetCurrentItem = function()
{
for(var x=0; x < this.appList.length; x++)
{
if(this.appList[x].isCurrent == true)
{
return this.appList[x];
}
return null;
}
}
}
function AppItem(name, appIconElementId, appPanelElementId)
{
this.name = name;
this.appIconElementId = appIconElementId;
this.appPanelElementId = appPanelElementId;
this.isCurrent = false;
}
</script>
</head>
<body onLoad="javaScript:AppInit();>
Reference Test
<body>
i am a oo (c#) programmer, and have not used javascript for a while and
i cant work out how javascript manages its references. Object
References work for simple stuff, but once i have an object collection
and stanrd using it it starts to fall apart.
Clearly there is something about javascript's usage of passing "By ref"
that i am not getting. i have had a look on the web and found some
examples, but i cant see why my code does not work. Grrrrh. I really
need to know why because i am writng a large app in javascript and
Ajax.
I program in c# mainly and i am applying soem OO principles to my
javascript but i am goign nust with how its works sometimes.
Also on a side not i dont get why i code javaScript this way to make it
work?
var personEntity = new PersonEntity("ged", 35);
document.write(personEntity.Name;
function PersonEntity(name, age)
{
var Name = name;
var Age - age;
}
it has to be instead:
function PersonEntity(name, age)
{
this.Name = name;
this.Age = age;
}
i have a sneaking suspicion that the above example also has something
to do with my misunderstanding of the javascript reference pointers
work.
Code is below as a complete test. You just have to save it and load
into your browser and it will run telling you where it errored. I
really want an answer on this and am trying to make it easy for someone
to tell me why is DOES NOT WOKR .
thansk in advance
Ged
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Reference Test</title>
<script language="JavaScript">
// Constants maps to static UI element ids
var APP_ICON_XPLORER_ID = "XplorerAppIcon";
var APP_ICON_GIMP_ID = "GimpAppIcon";
var APP_PANEL_XPLORER_ID = "XplorerAppPanel";
var APP_PANEL_GIMP_ID = "GimpAppPanel";
var g_appController = new AppController();
var g_appModel = new AppModel();
function AppInit()
{
// create app objects to map Dom Elements to other aspects
var xplorerApp = new AppItem('Xplorer', APP_ICON_XPLORER_ID,
APP_PANEL_XPLORER_ID);
g_appModel.Add(xplorerApp);
var gimpApp = new AppItem('Gimp', APP_ICON_GIMP_ID,
APP_PANEL_GIMP_ID)
g_appModel.Add(gimpApp);
// DO test of item being clicked
g_appController.AppItemLoadCommand(APP_ICON_XPLORER_ID);
}
function AppController()
{
// App Icon clicked
this.AppItemLoadCommand = function(id)
{
// resolve item clicked
var appItem = g_appModel.GetAppItemByIconElementId(id);
document.write("appItem.appIconElementId : " +
appItem.appIconElementId + "<BR />");
// Set Model
g_appModel.SetCurrentItem(appItem);
// Set View
var appView = AppView();
appView.SetFocus(g_appModel.appList, appItem);
}
}// JScript File
function AppModel()
{
this.currentItem = null;
this.appList = new Array();
this.Add = function(appItem)
{
this.appList[this.appList.length] = appItem;
};
this.GetAppItemByIconElementId = function(id)
{
for(var x=0; x < this.appList.length; x++)
{
if(this.appList[x].appIconElementId == id)
{
return this.appList[x];
}
return null;
}
}
this.GetAppItemByPanelElementId = function(id)
{
for(var x=0; x < this.appList.length; x++)
{
if(this.appList[x].appPanelElementId == id)
{
return this.appList[x];
}
return null;
}
}
this.SetCurrentItem = function(appitem)
{
document.write("Testing object passed by reference now <BR
/>");
// !! This fails to have the object passed in - whats gives
?//
// Gtes appItem is Not defines in javascript console //
document.write("appItem.appIconElementId : " +
appItem.appIconElementId);
appItem.isCurrent = true;
}
this.GetCurrentItem = function()
{
for(var x=0; x < this.appList.length; x++)
{
if(this.appList[x].isCurrent == true)
{
return this.appList[x];
}
return null;
}
}
}
function AppItem(name, appIconElementId, appPanelElementId)
{
this.name = name;
this.appIconElementId = appIconElementId;
this.appPanelElementId = appPanelElementId;
this.isCurrent = false;
}
</script>
</head>
<body onLoad="javaScript:AppInit();>
Reference Test
<body>