0

In the comments of this question, I was told that the .getOwnPropertyDescriptor() method

isn't supported in ES3 ..., so it probably isn't supported in JScript [either]

and that is indeed what I see when trying to invoke that method in cscript.exe/wscript.exe:

Object doesn't support this property or method

However, the latest JScript version I'm using is 5.812 and according to this document, the method should be available in 5.8* JScript. The discrepancy has also been noted in this post, pointing towards another post where a workaround using htmlfile COM object has been provided to access the missing properties/methods in Windows Script Host (WSH) JScript.

I was wondering if it is possible to use the same method to access the above method is WSH JScript as well.

For example, the code should be like

var object1 = {
  property1: 42
};

var htmlDoc = WScript.CreateObject('htmlfile');

// other code

var descriptor1 = <htmlfileObject>.getOwnPropertyDescriptor(object1, 'property1');
Wscript.StdOut.WriteLine(descriptor1.value);

Thanks for your support in advance.

P.S. I tag VBScript here as well because if someone knows how to do this in VBScript most probably we can easily convert it to JScript.

Foad S. Farimani
  • 12,396
  • 15
  • 78
  • 193
  • I’m not sure how we can be more clear but you can’t use features above ECMAScript Version 3 in JScript. The last update to the Active Scripting version of JScript was in IE9 which use ECMAScript Version 3. After that there is a version that sat side by side it based on a different engine (Chakra) that sacrifices support for Active Scripting features for the jsRT API. The JScript version you are using 5.8 came out in March 2009 with IE8 based on an implementation of ECMAScript Version 3 and was the JavaScript equivalent of Version 1.5. Sorry, but what you are trying to do can’t be done. – user692942 Dec 09 '20 at 00:55
  • I understand your [argument here](https://www.reddit.com/r/learnjavascript/comments/k9b7ca/implementing_pythons_dir_for_classic_jscript/?utm_content=post&utm_medium=twitter&utm_source=share&utm_name=submit&utm_term=t3_k9b7ca) but is just not supported. What version of JScript you actually use in an IE browser is dependent on the document mode support see Page 8 of [Microsoft JScript ECMA-262-1999 ECMAScript Language Specification Standards Support Document](https://interoperability.blob.core.windows.net/web/MS-ES3/%5BMS-ES3%5D-180323.pdf). – user692942 Dec 09 '20 at 12:14
  • So to access JScript 5.8 (including the support ECMA extensions for 5th Edition) you have to be running the browser in IE8 document mode. If you then want to work with this in Windows Scripting Host you are out of luck, because the extensions were only implemented for the browser, not the WSH. Unfortunately, JScript / VBScript / WSH are all legacy technologies that still work "as is", but when it comes to more modern methods and trying to support them it's a case of move on to more modern technologies. – user692942 Dec 09 '20 at 12:18
  • I’m happy to admit when I’m wrong and in this instance I am as [Kul-tigin's answer proves](https://stackoverflow.com/a/65216915/692942). Upvoted. – user692942 Dec 09 '20 at 14:28

1 Answers1

3

... However, the latest JScript version I'm using is 5.812 and ...

Actually that is the version of Windows Script Host, not the engine JScript.

In WSH, the term JScript is by default nothing but an alias / moniker for Microsoft's JavaScript engine that compatible with the standard ECMA-262 3rd edition.

Besides that default engine, you can use Chakra Engine (requires Edge) with WSH by specifying the engine's CLSID: 1b7cd997-e5ff-4932-a7a6-2a9e636da385.

Command to test if the engine is installed on the computer:

reg QUERY HKCR\CLSID\{1b7cd997-e5ff-4932-a7a6-2a9e636da385} /s

Example output on a computer Chakra installed:

HKEY_CLASSES_ROOT\CLSID\{1b7cd997-e5ff-4932-a7a6-2a9e636da385}
    (Default)    REG_SZ    JScript Language

HKEY_CLASSES_ROOT\CLSID\{1b7cd997-e5ff-4932-a7a6-2a9e636da385}\InprocServer32
    (Default)    REG_SZ    C:\Windows\System32\Chakra.dll
    ThreadingModel    REG_SZ    Both

test.js:

var object1 = {
  property1: 42
};

var descriptor1 = Object.getOwnPropertyDescriptor(object1, 'property1');

WScript.StdOut.WriteLine(descriptor1.value);

Command to run test.js with Chakra engine:

cscript //NoLogo //E:{1b7cd997-e5ff-4932-a7a6-2a9e636da385} test.js

Example output:

42
Kul-Tigin
  • 16,728
  • 1
  • 35
  • 64
  • That’s new to me didn’t realise it was possible to run Chakra from WSH. Excellent answer as always. – user692942 Dec 09 '20 at 14:05
  • 2
    @Lankymart Thanks. As you might guess it's not documented, and I don't think it's encouraged either. From my experience I can say that it's OK to use it with Windows scripts as the OP needs but definitely not a good idea to try to use it with ASP Classic since many ASP interfaces are not implemented in this engine. I tried it and it didn't go well ¯\_(ツ)_/¯ – Kul-Tigin Dec 09 '20 at 14:25
  • I’m guessing it wouldn’t work in a HTA because the engine is determined by the document mode not an engine switch? – user692942 Dec 09 '20 at 14:27
  • 1
    @Lankymart I agree. Also, even we manage to run it in HTA, I'm not sure all required namespaces,objects (window, document etc.) and the events work properly, as in the ASP Classic. – Kul-Tigin Dec 09 '20 at 14:38
  • is it also possible to use IE's engine Trident/MSHTML? I couldn't find the '.dll' nor the CLSID yet. – Foad S. Farimani Dec 09 '20 at 20:12
  • @Foad that isn’t a scripting engine. – user692942 Dec 09 '20 at 21:03