JScript for the .NET Framework


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
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 as to demonstrate the functionality.
1
class Test {  
2
    function run() {  
3
        var a=[1,[2,3,[4,5]]];  
4
        print(a.toString());  
5
        Array.prototype.toString=function()  
6
        {  
7
            return this.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:library test.js 
To call test.dll, I created test_tester.js like this:
1
var t = new Test();  
2
t.run(); 
And I compiled test_tester.js to an exe like so:
C:\Dev\Test1>jsc.exe /fast- /r:test.dll test_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
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 as to demonstrate the functionality.
1
class Test {  
2
    function run() {  
3
        var a=[1,[2,3,[4,5]]];  
4
        print(a.toString());  
5
        Array.prototype.toString=function()  
6
        {  
7
            return this.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:library test.js 
To call test.dll, I created test_tester.js like this:
1
var t = new Test();  
2
t.run(); 
And I compiled test_tester.js to an exe like so:
C:\Dev\Test1>jsc.exe /fast- /r:test.dll test_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
Hallo Des,thank for your answer.Yes, your code works fine, my troubles arise when I try to call the same test.dll from standart c# console project.I added reference to .NET  Microsoft.Jscript in my 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 add reference to  test.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 think project should know the file when I add it by standard "Add reference dialog"Anyway when I manually copy test.dll from original directory to C# project  /Bin/Debug directoryit 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
Hi Marek,Sorry, it lookes like you won't be able to reference this DLL from a C# project specifically because a global prototype has been modified.  You could redefine the toString function on each instance separately...Des
Ok, thank you for your answers.Marek 
Any further detail on this; why does a global prototype modification cause the library to not be usable as a .NET assembly?
Regards, Fil.

Related Links

URGENT: Time Session for my Internet Banking Project
InfoPath JScript - Loop through repeating elements in a Data Source programmatically
jquery intellesnese
To display a Yes/No dialog box after reading a file !!
Is there a VBScript for .NET?
Microsoft.JScript.Vsa.VsaEngine
HOWTO set a AJAX:TabContainer's height in javascript at run time.
Consume Web Service with Transport Layer Security (HTTPS) from JavaScript
Multi-threading in javascript
Click handler on popup does not run properly in a .NET environment
How to update ASP.NET image control periodically?
Format date before SQL Query execution
Alert window in aspx page.
Class doesn't support automation - or - how to install WS 5.7 on an XP SP3
how to do validation in cshtml (MVC)
How to implement IObjectSafety programmatically in JScript.NET

Categories

MSDN
Windows Phone Development
.NET Framework
Visual Studio Design a...
Architecture
Commerce Server
Smart Device Development
Windows Hardware Devel...
Bing Maps
Open Specifications
Exchange Server
Archived Forums V
Skype for Business
Software Entwicklung f...
MSBuild
Register and Manage Ac...
Application Compatibil...
Developing Windows Des...
Media Foundation Devel...
Windows Desktop Pro-Au...
Windows Hardware Testi...
Hardware Development B...
SharePoint 2013 - Deve...
SharePoint 2013 - Search
Service Bus
Azure Media Services
Microsoft Azure Site R...
Azure Backup
Azure HDInsight
Azure Cache
Azure Notification Hubs
Azure Data Factory
Azure Log Analytics
StorSimple
Claims based access pl...
ADO.NET Managed Providers
ADO.NET Entity Framewo...
LINQ to SQL
XML, System.Xml, MSXML...
Microsoft Translator H...
Partnering with Microsoft
Microsoft Robotics - C...
Microsoft Robotics - S...
SQL Server Integration...
SQL Server High Availa...
SQL Server Data Wareho...
SQL Server Search
SQL Server Spatial
Database Design
Power Pivot
SQL Server Data Qualit...
Standard 2009: Tools
Standard 7: Misc
Windows Embedded 8.1 I...
Windows Embedded Compa...
Bing Maps: Map Control...
Windows Live Writer: D...
The Samsung Windows De...
Bing Maps General
Office Protocols
Project Server - Setup...
Report a Silverlight Bug
Accessing Web Services...
Silverlight for Window...
Designing with Silverl...
Expression Design
Connected Services Fra...
Retired SharePoint Col...
.NET Framework 4: Wind...
Audio API and Programm...
Direct3D 10
Dynamic Languages on .NET
DNS and Traffic Manager
Game Technologies: Gen...
Git, Mercurial and Dro...
Innovate on Office
ISV Community Days
LINQ Project General
Model-Based Testing wi...
Microsoft "Data Explor...
Network Quality of Ser...
Office Live Small Busi...
NUI API and Programmin...
Search Server: Frequen...
Search, Organize, and ...
Sharepoint Development...
Silverlight (formerly ...
Share your How to samp...
Software Transactional...
Surface Application De...
SQL Server 2014 Data Q...
SQL Server 2014 In Azu...
SQL Server 2014 Analys...
SQL Server 2014 Integr...
Team Foundation Server...
Transactions Programming
Visual Basic for Appli...
Visual Basic IDE
Visual Basic Interop a...
Visual C++ 2008 (Pre-r...
Visual Studio Team Ser...
Visual Studio 2008 Exp...
Visual Studio Class De...
Visual Studio Code Ana...
Visual Studio Lab Mana...
Visual Studio LightSwi...
Visual Studio Team Sys...
Windows Events
Windows Live Search: D...
Windows SideShow Devel...
Working with Data and ...
Tools for Windows Stor...
XML Paper Specificatio...
Azure N-Series Virtual...

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile