我的脚本尖锐操作被重命名,因此它们以下划线为前缀。尽管代码在浏览器中运行得很好,但必须记住添加下划线才能在客户端使用它,这很烦人。这是故意的吗?有没有办法改变它?
以下是一个示例:我已经复制了Knockout JS的教程示例"点击计数器"。
脚本Sharp View模型(C#代码):
public sealed class ClickCounterViewModel
{
public Observable<int> numberOfClicks;
//Dependent Observable is now called computed but is backward compat.
public DependentObservable<bool> hasClickedTooManyTimes;
//WARNING - this get converted to _registerClick Client Side - not sure why.
Action registerClick;
Action resetClicks;
public ClickCounterViewModel()
{
numberOfClicks = Knockout.Observable<int>(0);
registerClick = delegate() {
this.numberOfClicks.SetValue(this.numberOfClicks.GetValue() + 1);
};
resetClicks = delegate() { this.numberOfClicks.SetValue(0); };
DependentObservableOptions<bool> options = new DependentObservableOptions<bool>();
options.Model = this;
options.GetValueFunction = new Func<bool>(delegate {
return this.numberOfClicks.GetValue() >= 3;
});
hasClickedTooManyTimes = Knockout.DependentObservable<bool>(options);
}
}
当此代码转换为javascript时,操作将以下划线为前缀。这是预期的行为吗?
生成的代码(javascript)-只显示生成的注释来说明问题:
Knockout2Example2.ClickCounterViewModel = function Knockout2Example2_ClickCounterViewModel() {
/// <field name="numberOfClicks" type="Observable`1">
/// </field>
/// <field name="hasClickedTooManyTimes" type="DependentObservable`1">
/// </field>
/// <field name="_registerClick" type="Function">
/// </field>
/// <field name="_resetClicks" type="Function">
/// </field>
/// This script was generated using Script# v0.7.4.0
哎呀!
我没有公开我的两个Actions registerClick和resetClicks。公开它们可以解决这个问题,并且它们被呈现为不带下划线的js。
Undercore是一个常见的JavaScript约定,意思是"这是私有的"。Scriptsharp提供以下划线开头的非公共成员、属性和方法名称。公开您的操作,以便在外部JavaScript中方便地引用它们。