.NET Framework Bookmark and Share   
 index > JScript for the .NET Framework > Cannot redefine default prototype function in jscript.net class
 

Cannot redefine default prototype function in jscript.net class

Hallo,

I have following code in Jscript.net

//-------------------------------------

Array.prototype.toString=function()
{
return this.join('');
}

var a=[1,[2,3,[4,5]]];

print(a.join(''));

//-----------------------------

When I compile this code with jsc.exe as an executable it gives
12345

I would like to include this code into a class and compile it as a library,
but dll throws an error when I call run function from console application
no matter if jscript code is compiled with /fast-

//---------------

class Test
{
public function run()
{
var a=[1,[2,3,[4,5]]];
Array.prototype.toString=function()
{
return this.join('');
}
return a.join('');
}
}

//---------------

Any idea how to make it?
Thank you.

Marek

MarekNovy

Hi Marek,

I tried your example and it works for me as follows:

I created test.js as you indicated but added lines 4 and 10 so asto demonstrate the functionality.

1 classTest{
2 functionrun(){
3 vara=[1,[2,3,[4,5]]];
4 print(a.toString());
5 Array.prototype.toString=function()
6 {
7 returnthis.join('');
8 }
9 print(a.join(''));
10 print(a.toString());
11 }
12 }



I copied jsc.exe to my working directory for convenience and compiled test.dll as follows:

C:\Dev\Test1>jsc.exe/fast-/t:librarytest.js


To call test.dll, I created test_tester.js like this:

1 vart=newTest();
2 t.run();



And I compiled test_tester.js to an exe like so:

C:\Dev\Test1>jsc.exe/fast-/r:test.dlltest_tester.js



Finally, running test_tester.exe, this is the output I get:

C:\Dev\Test1>test_tester.exe
1,2,3,4,5
12345
12345
C:\Dev\Test1>


Does that help?

Des



Mr Deslock

Hi Marek,

I tried your example and it works for me as follows:

I created test.js as you indicated but added lines 4 and 10 so asto demonstrate the functionality.

1 classTest{
2 functionrun(){
3 vara=[1,[2,3,[4,5]]];
4 print(a.toString());
5 Array.prototype.toString=function()
6 {
7 returnthis.join('');
8 }
9 print(a.join(''));
10 print(a.toString());
11 }
12 }



I copied jsc.exe to my working directory for convenience and compiled test.dll as follows:

C:\Dev\Test1>jsc.exe/fast-/t:librarytest.js


To call test.dll, I created test_tester.js like this:

1 vart=newTest();
2 t.run();



And I compiled test_tester.js to an exe like so:

C:\Dev\Test1>jsc.exe/fast-/r:test.dlltest_tester.js



Finally, running test_tester.exe, this is the output I get:

C:\Dev\Test1>test_tester.exe
1,2,3,4,5
12345
12345
C:\Dev\Test1>


Does that help?

Des



Mr Deslock

Hallo Des,

thank for your answer.
Yes, your codeworks fine,my troubles arise when I try to call the same test.dll from standart c# console project.
I addedreference to .NET Microsoft.Jscript inmy C# project.

using System;
using Microsoft.JScript;
using Microsoft.JScript.Vsa;

namespace tester
{
class Program
{
static void Main(string[] args)
{
Test test = new Test();
test.run();
}
}
}


When I browse and addreference totest.dll
C# project compiles ok but at run time it throws:

System.IO.FileNotFoundException was unhandled
Message="Could not load file or assembly 'test, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. "
Source="tester"
FileName="test, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"

I thinkproject should know the file when I add it by standard "Add referencedialog"

Anyway when I manually copy test.dll from original directory to C# project/Bin/Debug directory
it again compiles ok but at run time it throws:

Message="Unable to cast object of type 'Microsoft.JScript.GlobalObject' to type 'Microsoft.JScript.LenientGlobalObject'."
Source="Microsoft.JScript"

I use VS 2005 proffessional and .NET 2.0.50727

Thank you for more help on this.

Marek

MarekNovy
Hi Marek,

Sorry, it lookes like you won't be able to referencethis DLLfrom a C# project specifically because a global prototype has been modified. You could redefine the toString function on each instance separately...

Des
Mr Deslock


Ok, thank you for your answers.
Marek

MarekNovy

You can use google to search for other answers

Custom Search

More Threads

• Disable all controls on a web page
• How can i automate my build process.
• ConfigurationManages namespace
• Why is string parameter being broken up by WScript.Arguments?
• Open SELECT list with script
• Jscript and .NET Framework Upgrade
• I'm not an expert, so please forgive me...
• how to install suport for jscript into visual web developer 2008 express edition?
• Passing window argument in dialgoArguments.window.open
• Google Maps, RegisterClientScriptBlock and getElementById