473,795 Members | 2,968 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem with accessing functions

hello,

main script creates IE window, put an array there and then calls a child
script. chils script makes an array element equal to some object and
returns control to main script. then main script doesn't see that object
and its functions. why?

here is the code I implemented:

//in main script
oWSH = new ActiveXObject(W Script.Shell);
oIEWindow.docum ent.Script.MyAr ray = new Array()
oWSH.Run("child .js", 2, true);

//in child.js script
var MyObj = function() {
var Value = 3.14159;
var getValue = function() { return Value; }
var setValue = function(arg) { Value = arg; }
this.gV = getValue;
this.sV = setValue;
}
oIEWindow.docum ent.Script.MyAr ray[0] = new MyObj();
oIEWindow.docum ent.Script.MyAr ray[0].sV(2.71); // <-- this works
oIEWindow.docum ent.Script.MyAr ray[0].gV(); // <-- this works

//returning back to main
oIEWindow.docum ent.Script.MyAr ray[0].gV() // <-- this doesn't work

is it possible to set an object in array and call its functions from a
child script and then call'em again from main script?

what did I assume wrong in this code?

please help,
regards,
mirek

ps. I also tried the following definition of MyObj with no success:

var MyObj = function() {
mo = new Object();
mo.Value = 3.14159;
mo.gV = function() { return mo.Value; }
mo.sV = function(arg) { mo.Value = arg; }
return mo;
}

Jul 20 '05 #1
8 2353
In article <4G************ **********@news .chello.at>,
no*********@ant ispam.address enlightened us with...
hello,

main script creates IE window, put an array there and then calls a child
script. chils script makes an array element equal to some object and
returns control to main script. then main script doesn't see that object
and its functions. why?

[guess]
It's probably out of scope.

here is the code I implemented:

//in main script
oWSH = new ActiveXObject(W Script.Shell);
oIEWindow.docum ent.Script.MyAr ray = new Array()
oWSH.Run("child .js", 2, true);

//in child.js script
var MyObj = function() {
This var MuObj is not global (?). It will go out of scope and become
nothing when the child finishes. You don't return the object to a var in
the parent, like

var myObj = oWSH.Run("child .js",2,true);

That's a guess - I've never really done OOP JS. I do this in java,
though.

is it possible to set an object in array and call its functions from a
child script and then call'em again from main script?


Well, I had a problem in JSP where the object being placed in the array
was actually a reference, not a copy, and the reference became nothing
when the scope reverted to main. I can't say if that's a problem for you
or not, as I've never tried this in JS.

-------------------------------------------------
~kaeli~
Press any key to continue or any other key to quit.
Who is General Failure and why is he reading
my hard disk?
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
-------------------------------------------------
Jul 20 '05 #2
hi,
[guess]
It's probably out of scope.


well, it looks like.. but I don't really understand.. the second
definition returns the object. I mean that "MyArray[0]" contains MyObj
object with its all methods (as I understand it). I also somehow
understand that first definition (without var mo = new Object()) can
loose its properties and methods, because it doesn't return anything to
MyArray[0] (but I am beginner and I doubt myself)

on the other side when I do a small change in code, namely I set
MyArray[0] in main script:

//in main script
oWSH = new ActiveXObject(W Script.Shell);
oIEWindow.docum ent.Script.MyAr ray = new Array()
oIEWindow.docum ent.Script.MyAr ray[0] = new MyObj(); // <-- added line
oWSH.Run("child .js", 2, true);

and then call child.js, everything works fine. from child.js I can call
MyArray[0].somefunction() and results are visible in main.js after
returning back.

generally I don't understand what's visible, what's not, and why in this
example..

regards,
mirek

Jul 20 '05 #3
In article <AS************ **********@news .chello.at>,
no*********@ant ispam.address enlightened us with...

well, it looks like.. but I don't really understand.. the second
definition returns the object.
Not to main, it doesn't. It returns it to another function in child. The
reference goes away when child is done.
on the other side when I do a small change in code, namely I set
MyArray[0] in main script:

//in main script
oWSH = new ActiveXObject(W Script.Shell);
oIEWindow.docum ent.Script.MyAr ray = new Array()
oIEWindow.docum ent.Script.MyAr ray[0] = new MyObj(); // <-- added line
oWSH.Run("child .js", 2, true);

and then call child.js, everything works fine. from child.js I can call
MyArray[0].somefunction() and results are visible in main.js after
returning back.

Then the problem is indeed scope. You made a variable local to main and
child merely changed it, so it stayed there.
Using the "new" keyword makes a new object, so that object would be
local to child or main depending on where you put it.

Here's a couple good explanations of scope.
http://sharkysoft.com/tutorials/jsa/content/031.html
http://www.javaworld.com/javaworld/j...avascript.html
generally I don't understand what's visible, what's not, and why in this
example..


Well, scope works pretty much the same (excluding blocks in C) in all
the languages I've used...

outside any function...
var v = something;
v is global and visible anywhere

in function f...
var v = something;
v is local to f and not visible anywhere else.

in function g...
var x = v;
will fail if v was declared in f, will succeed if declared outside a
function.

So, if you have a main file with functions and statements, variables
declared outside any function are global and variables declared in a
function are local to that function and go away when the function is
done.

I am only vaguely familiar with WSH, so I am not sure what Run does. I
will assume it parses / executes "child.js" - therefore, anything in
child.js stays there. MyObj would be local to child.js and go away when
execution finishes (return control to main).

The solution, as you discovered, is to declare the object in main, then
execute the child.
You could probably also return the object and assign it to the array in
main, but I'm not certain. I don't think it would work the way you are
using the Run command, as I don't think you can return something that
way. If you were just using functions, it would work.

-------------------------------------------------
~kaeli~
Press any key to continue or any other key to quit.
Who is General Failure and why is he reading
my hard disk?
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
-------------------------------------------------
Jul 20 '05 #4
hello,
well, it looks like.. but I don't really understand.. the second
definition returns the object.
Not to main, it doesn't. It returns it to another function in child. The
reference goes away when child is done.


to what function..? :( I don't understand that.. because actually both
main and client operate on the third, separate object: IE window with
its array.

it looks like this:
main creates this IE window via new ActivexObject and puts array there.
main initialize the [0]-element of this array as an MyObj object. then
main calls client as a separate process. client sees this IE window --
it looks for it and finds. then client calls MyArray[0] methods (which
is MyObj now set by main), and returns control back. then again main
sees the client's changes in IE window.

alternatively it looks like this:
main creates this IE window via new ActivexObject and puts array there.
then main calls client as a separate process. client does
MyArray[0]=MyObj, calls its [0]-element methods etc. after returning
back to main changes in IE window are not visible.
Here's a couple good explanations of scope.
http://sharkysoft.com/tutorials/jsa/content/031.html
http://www.javaworld.com/javaworld/j...avascript.html
surely I will put an eye there. thanks. :)
Well, scope works pretty much the same (excluding blocks in C) in all
the languages I've used...
yes, but this is about "global" and "local" scope for *separate
processes*, not for functions.. does it propagate similar way?
I am only vaguely familiar with WSH, so I am not sure what Run does. I
will assume it parses / executes "child.js" - therefore, anything in
child.js stays there. MyObj would be local to child.js and go away when
execution finishes (return control to main).
no, no.. Run executes another script as a separate process. as I
understand (please correct me), it is *completely separate*, except
envoironment variables in "USER" space. any communication between these
processes is then via this IE window, visible for main and client.
The solution, as you discovered, is to declare the object in main, then
execute the child.
yes, but that ruins my algorithm a little :)
You could probably also return the object and assign it to the array in
main, but I'm not certain. I don't think it would work the way you are
using the Run command, as I don't think you can return something that
way. If you were just using functions, it would work.


yes, it works for functions. but it is about processes. I would like to
return an object created in one process to another process via third
window, visible to both of them.

thanks ~kaeli~, I will look at those urls. As I know inter processes
communication differ from function-2-function.. If you think that I'm
wrong, please tell me..

regards,
mirek

Jul 20 '05 #5
In article <ST************ **********@news .chello.at>,
no*********@ant ispam.address enlightened us with...
to what function..? :( I don't understand that.. because actually both
main and client operate on the third, separate object: IE window with
its array.


That depends on the setup. From what you say below, I misworded my reply
because I didn't understand the whole of what you were doing. I kinda
still don't. :)

If you program in C, I could explain by saying you aren't extern-ing
your vars...
it looks like this:
main creates this IE window via new ActivexObject and puts array there.
main initialize the [0]-element of this array as an MyObj object. then
main calls client as a separate process.
Key words - separate process. That process has its own space for things
(memory), often called a "heap". The "new" keyword allocates the space
on the *current* heap - the child's.
client sees this IE window --
it looks for it and finds. then client calls MyArray[0] methods (which
is MyObj now set by main), and returns control back. then again main
sees the client's changes in IE window.

Main sees it because it was allocated during main and is in main's
memory heap (scope).
alternatively it looks like this:
main creates this IE window via new ActivexObject and puts array there.
Array is a pointer to a block of memory. Empty memory. Just a space to
put stuff.
(js people correct me if I'm wrong, 'cuz I do this stuff in java and c)
then main calls client as a separate process.
Process gets its own heap. Allocates new objects to that space.
But the problem is this:
oIEWindow.docum ent.Script.MyAr ray[0] = new MyObj();

When that runs from child, MyObj points to a spot in the *child's* heap
- which disappears when the child process exits!
The array then points to dead space.
client does
MyArray[0]=MyObj, calls its [0]-element methods etc. after returning
back to main changes in IE window are not visible.

MyObj was allocated to the heap in child. When child process dies, heap
goes bye-bye. Array now points to nothing.

yes, but this is about "global" and "local" scope for *separate
processes*, not for functions.. does it propagate similar way?

Yes.
As long as main is active, its heap is viable.
The solution, as you discovered, is to declare the object in main, then
execute the child.


yes, but that ruins my algorithm a little :)


Solutions often do. :)

yes, it works for functions. but it is about processes. I would like to
return an object created in one process to another process via third
window, visible to both of them.


There has to be a way, but I don't know it...

I'm now over my head a bit - did you try
microsoft.publi c* newsgroups? They do a bit more WSH and processes stuff
with IE than most...

-------------------------------------------------
~kaeli~
Press any key to continue or any other key to quit.
Who is General Failure and why is he reading
my hard disk?
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
-------------------------------------------------
Jul 20 '05 #6
hi,
alternative ly it looks like this:
main creates this IE window via new ActivexObject and puts array there.


Array is a pointer to a block of memory. Empty memory. Just a space to
put stuff.
(js people correct me if I'm wrong, 'cuz I do this stuff in java and c)
then main calls client as a separate process.


Process gets its own heap. Allocates new objects to that space.
But the problem is this:
oIEWindow.docum ent.Script.MyAr ray[0] = new MyObj();

When that runs from child, MyObj points to a spot in the *child's* heap
- which disappears when the child process exits!
The array then points to dead space.
client does
MyArray[0]=MyObj, calls its [0]-element methods etc. after returning
back to main changes in IE window are not visible.


MyObj was allocated to the heap in child. When child process dies, heap
goes bye-bye. Array now points to nothing.


well.. if it is as you say then it starts to convice me :) Let me
summarize it:

main creates IE Window, and says "let there be an array". this is just
pointing to some place in memory, the beginning of the first element,
but "length" and it's end are unknown yet. then main executes MyArray[0]
= new MyObj() and by doing so, it really reserves a chunk of memory and
fills it with MyObj() elements. then main calls client. client can see
the object, even created by another process because MyObj[0] points
where MyObj *really* exists. after returning back mains sees the changes.

the alternative (doesn't work):
[...]unknown yet. then main calls client.js. client reserves and fills a
chunk of memory with MyObj() but this is at its own memory space.
(please correct me: if for example, in this very moment yet another
process wanted to execute MyArray[0] (via IE window) it would so
succesfully, right? because this object now *really* exists in memory,
and it doesn't matter where, because IE window MyArray[0] points exactly
there. right?). after returning back mains does not see the changes,
because MyArray[0] still points to a chunk of mamory, but this chunk
does not contain the object already.

right? :)

regards,
mirek

Jul 20 '05 #7
In article <Ql************ **********@news .chello.at>,
no*********@ant ispam.address enlightened us with...

well.. if it is as you say then it starts to convice me :) Let me
summarize it:

main creates IE Window, and says "let there be an array". this is just
pointing to some place in memory, the beginning of the first element,
No - even more abstract. It points to the place the first element would
be at if there were a first element - which there isn't.

This is C, so forgive me if JS is more forgiving with pointers and
arrays and objects...

var MyArray;

myArray is now simply declared and points to nothing and its value is
NULL/undefined.

var myArray = new Array();

myArray, to the computer, now points to some memory address, say 12345,
that is the start of a block of memory large enough to hold an Array
object. That memory address either has nothing or has garbage. What it
doesn't have is any meaningful value.
but "length" and it's end are unknown yet. then main executes MyArray[0]
= new MyObj() and by doing so, it really reserves a chunk of memory and
fills it with MyObj() elements.
When main says
myArray[0] = new myObj();
the computer goes to 12345 and blocks out a chunk of memory large enough
to fit a myObj object in and initializes it however the constructor for
myObj tells it to.
then main calls client. client can see
the object,
Client can see the object - as the object beginning at 12345 and ending
at 12345+size(myOb j).
even created by another process because MyObj[0] points
where MyObj *really* exists. after returning back mains sees the changes.
Yup.
Any changes made by child change the contents of memory block 12345+size
(myObj).

the alternative (doesn't work):
myArray points to nothing. It is null. It has no memory block assigned.
[...]unknown yet. then main calls client.js. client reserves and fills a
chunk of memory with MyObj() but this is at its own memory space.
Yup. The block is now 98765+size(myOb j).
(please correct me: if for example, in this very moment yet another
process wanted to execute MyArray[0] (via IE window) it would so
succesfully, right? because this object now *really* exists in memory,
and it doesn't matter where, because IE window MyArray[0] points exactly
there. right?).
You know, I can't be really sure on that. I don't do multithreading
well. Actually, I never do multithreading.
after returning back mains does not see the changes,


Because main still has array[0] pointing to 98765 - which no longer has
anything in it, since the child released it!

Does that help?

-------------------------------------------------
~kaeli~
Press any key to continue or any other key to quit.
Who is General Failure and why is he reading
my hard disk?
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
-------------------------------------------------
Jul 20 '05 #8
> Does that help?

yes! thanks a lot :)

regards,
mirek

Jul 20 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
2871
by: abs | last post by:
Hi all. My list: <ul> <li id="a" onclick="show(this)">Aaaaaaaa</li> <li id="b" onclick="show(this)">Bbbbbbbb</li> <li id="c" onclick="show(this)">Cccccccc <ul> <li id="d" onclick="show(this)">111111</li>
7
2040
by: Nicole | last post by:
Hi I'm trying to use a function to set a session variable. I have three files: The first file has: <?php session_start(); // This connects to the existing session ?> <html> <head>
0
1714
by: stefan | last post by:
hello everybody, I want to call a method on a SAP WAS. For testing purposes, I do have the same method on two WASs. One of them has been updated recently. The wsdl-files of both are not identical from a text point of view, however, from the xml-point of view they are (one declares all types inline, the other by type-attributes and subsequent definition). so, no wonder at all, both of them result in identical c#-proxyclasses after...
2
3455
by: pauldepstein | last post by:
I have probably "bitten off more than I can chew". But, I'm new to c++ and am trying to debug a program consisting of 9 files and about 2000 lines of code. My program compiles but I get a runtime error: "Access violation: segmentation fault raised in your program" -- dev c++ compiler. Here is the most specific indicator of what is going wrong: There follows a small sample of code.
3
1514
by: Fernando Rodríguez | last post by:
Hi, I'm writing code to validate fields in a form before saving to a db. All the validating functions are in a separate script which is required. All the validating functions add an error message to an array if the data doesn't validate. I check if something went wrong with count($theArray). Here's my code: ---------------------------------------------------------------------------
12
2517
by: zMisc | last post by:
I have a problem selecting records with \ in any values. For example: SELECT * FROM ADDRESS WHERE STREET = "A\" will give an error: If I replace \ with \\ then it works. Eg.
39
19650
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1) What's the difference between these 3 statements: (i) memcpy(&b, &KoefD, n); // this works somewhere in my code
3
4317
Digital Don
by: Digital Don | last post by:
I have a problem sending the Vectors as Referencial parameters..i.e. I am having a struct vector struct board { int r; int c; };
9
1953
by: fgh.vbn.rty | last post by:
Say I have a base class B and four derived classes d1, d2, d3, d4. I have three functions fx, fy, fz such that: fx should only be called by d1, d2 fy should only be called by d2, d3 fz should only be called by d1, d3, d4 I think I have two options. (1) Make all functions virtual and define them in the required derived classes. This will of course lead to a lot of code duplication and problems in maintainability.
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10439
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10165
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7541
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6783
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3727
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.