Using htmlfile COM object to access the .getOwnPropertyDescriptor() method of an object in WSH JScript

佐手、 提交于 2020-12-15 05:31:23

问题


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.


回答1:


... 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


来源:https://stackoverflow.com/questions/65207848/using-htmlfile-com-object-to-access-the-getownpropertydescriptor-method-of-an

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!