Can not use prototype declarations with V8 Engine

Nov 21, 2013 at 8:14 PM
Hi.

I've tried to create a simple "class" declaration using prototype declarations and it seems like that V8 engine don't support that.

The C# code is:
using (var engine = new JScriptEngine())
            {
                /* Attach the host functions */
                engine.AddHostObject("host", new HostFunctions());
                engine.AddHostObject("Environment", new HostTypeCollection("mscorlib", "System.Core"));
                
                var assembly = System.Reflection.Assembly.GetExecutingAssembly();
                using (var stream = assembly.GetManifestResourceStream("ClearScriptTests.Scripts.sample.js"))
                using (var reader = new System.IO.StreamReader(stream))
                {
                    engine.Execute(reader.ReadToEnd());
                }
           }
The JS code is (sample.js embedded resource):
var System = Environment.System;
var Console = System.Console;
var Car = (function () {
    function Car(logger, settings) {
    }

    Car.prototype.bar = function () { Console.WriteLine("bar"); }
}());
for (var m in Car) {
    Console.WriteLine("Property: " + m);
}
When I'm changing the engine from V8ScriptEngine to JScriptEngine the code works pefectly. I cane across this when trying to use TypeScript, which uses prototypes too.

Thanks for any feedback!
Coordinator
Nov 22, 2013 at 3:03 AM
Hello yahavgb!

We tested your code and got the same results with both script engines: Car is undefined and no output is generated. That makes sense because the code sets Car (the outer one) to the return value of a function that lacks a return statement.

What results are you seeing?

Thanks!