Does JScript support the string-fitting method?

When developing a Windows procedure using JScript, it seems that some of the string methods don't work. In this example using trim, line 3 generates a runtime error:

"The object does not support this property or method."

My code:

strParent = "  a  ";
strParent = strParent.trim();
WScript.Echo ("Value: " + strParent);

      

Am I stupid? Any ideas what the problem is?

+3


source to share


3 answers


You can add trim

to the String class:

trim-test.js

String.prototype.trim = function()
{
    return this.replace(/^\s+|\s+$/g, '');
};

strParent = "  a  ";
strParent = strParent.trim();
WScript.Echo ("Value: " + strParent);

      



Exit cmd.exe

C:\>cscript //nologo trim-test.js
Value: a

      

+4


source


JScript running under the Windows Scripting Host uses an older version of JScript based on ECMAScript 3.0. The trim function was introduced in ECMAScript 5.0.



+1


source


Use a polyfill like this one: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim#Polyfill

This snippet:

if (!String.prototype.trim) {
  String.prototype.trim = function () {
    return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
  };
}
strParent = "  a  ";
strParent = strParent.trim();
WScript.Echo ("Value: '" + strParent + "'");

      

deduces

Value: 'a'

      

+1


source







All Articles