设置CDO.Message选项时使用的f(x)= y JScript习语的JavaScript友好替代

时间:2022-01-01 01:55:08

I have an ASP page written in JScript that sends e-mails using CDO.Message. For specifying an SMTP server (and other options) I'm doing something like this:

我有一个用JScript编写的ASP页面,它使用CDO.Message发送电子邮件。为了指定SMTP服务器(和其他选项),我正在做这样的事情:

mail.Configuration.Fields.Item(
    "http://schemas.microsoft.com/cdo/configuration/smtpserver") =
    "smtp.example.com";

Now, here comes the catch. I have this code in a stand-alone include file that I include in an HTML page as JavaScript so that I can run unit tests against it in a browser (using JsUnit etc.). I have JavaScript mock objects (Server, Request, etc.) that create a mock ASP environment for the included JScript code. The only problem I have left is with the CDO.Message option setting. Since the f(x) = y syntax that's used in the above code excerpt is not valid JavaScript (invalid left-hand operand), I can't run this piece of code (as it is) within a browser. I'm currently simply bypassing it in my unit test with a conditional that detects whether the environment is truly ASP.

现在,抓住这里了。我将此代码放在一个独立的包含文件中,我将其作为JavaScript包含在HTML页面中,以便我可以在浏览器中使用JsUnit等对其进行单元测试。我有JavaScript模拟对象(服务器,请求等),它们为包含的JScript代码创建了一个模拟ASP环境。我留下的唯一问题是使用CDO.Message选项设置。由于在上面的代码摘录中使用的f(x)= y语法不是有效的JavaScript(无效的左手操作数),因此我无法在浏览器中运行这段代码(实际上)。我目前只是在我的单元测试中绕过它,条件是检测环境是否真的是ASP。

I don't think that there's a JavaScript workaround to this. I'm looking for an alternative syntax (that may use the ActiveX interfaces differently) to setting CDO.Message options that would also be syntactically valid JavaScript.

我不认为这有一个JavaScript解决方法。我正在寻找一种替代语法(可能使用不同的ActiveX接口)来设置CDO.Message选项,这些选项也可以是语法上有效的JavaScript。

2 个解决方案

#1


I figured out the answer when looking at the C++ code example at http://msdn.microsoft.com/en-us/library/ms526318(EXCHG.10).aspx.

在查看http://msdn.microsoft.com/en-us/library/ms526318(EXCHG.10).aspx上的C ++代码示例时,我想出了答案。

The solution is to make the assignment explicitly to the Value property:

解决方案是明确赋值给Value属性:

mail.Configuration.Fields.Item(
    "http://schemas.microsoft.com/cdo/configuration/smtpserver").Value =
    "smtp.example.com";

This way, the code above is valid JavaScript than can be tested with a mock Configuration object.

这样,上面的代码是有效的JavaScript,而不是使用模拟Configuration对象进行测试。

#2


I've been having the same problem when writing server-side Javascript for IIS, That f(x) = y syntax was failing in my IDE's syntax checker. The solution I found helpful was JScript conditional comments like so:

我在为IIS编写服务器端Javascript时遇到了同样的问题,f(x)= y语法在我的IDE语法检查器中失败了。我发现有用的解决方案是JScript条件注释,如下所示:

f(x)/*@cc_on@if(0)*/[0]/*@end@*/ = y;

It puts the subscript index [0] on the end except when running in Microsoft's JScript engine. But, admittedly my solution is a bit hacky. I think in most cases yours is cleaner, so thanks for sharing it.

它将下标索引[0]放在最后,除非在Microsoft的JScript引擎中运行。但是,诚然,我的解决方案有点hacky。我认为在大多数情况下你的更干净,所以感谢分享它。

-Simon

#1


I figured out the answer when looking at the C++ code example at http://msdn.microsoft.com/en-us/library/ms526318(EXCHG.10).aspx.

在查看http://msdn.microsoft.com/en-us/library/ms526318(EXCHG.10).aspx上的C ++代码示例时,我想出了答案。

The solution is to make the assignment explicitly to the Value property:

解决方案是明确赋值给Value属性:

mail.Configuration.Fields.Item(
    "http://schemas.microsoft.com/cdo/configuration/smtpserver").Value =
    "smtp.example.com";

This way, the code above is valid JavaScript than can be tested with a mock Configuration object.

这样,上面的代码是有效的JavaScript,而不是使用模拟Configuration对象进行测试。

#2


I've been having the same problem when writing server-side Javascript for IIS, That f(x) = y syntax was failing in my IDE's syntax checker. The solution I found helpful was JScript conditional comments like so:

我在为IIS编写服务器端Javascript时遇到了同样的问题,f(x)= y语法在我的IDE语法检查器中失败了。我发现有用的解决方案是JScript条件注释,如下所示:

f(x)/*@cc_on@if(0)*/[0]/*@end@*/ = y;

It puts the subscript index [0] on the end except when running in Microsoft's JScript engine. But, admittedly my solution is a bit hacky. I think in most cases yours is cleaner, so thanks for sharing it.

它将下标索引[0]放在最后,除非在Microsoft的JScript引擎中运行。但是,诚然,我的解决方案有点hacky。我认为在大多数情况下你的更干净,所以感谢分享它。

-Simon