小编Ric*_*ard的帖子

TypeScript中的私有"函数"

是否可以在TypeScript类中创建私有"函数"(方法)?假设我们有以下Person.tsTypeScript文件:

class Person {
    constructor(public firstName: string, public lastName: string) {
    }

    public shout(phrase: string) {
        alert(phrase);
    }

    private whisper(phrase: string) {
        console.log(phrase);
    }
}
Run Code Online (Sandbox Code Playgroud)

在编译时将其转换为以下内容:

var Person = (function () {
    function Person(firstName, lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
    Person.prototype.shout = function (phrase) {
        alert(phrase);
    };
    Person.prototype.whisper = function (phrase) {
        console.log(phrase);
    };
    return Person;
})();
Run Code Online (Sandbox Code Playgroud)

意见

我期待whisper函数在闭包内声明,但不是在原型上?从本质上讲,这会使whisper函数在编译时公开?

typescript

71
推荐指数
2
解决办法
6万
查看次数

JavaScript中的可选参数

为可选参数分配默认值的好方法是什么?

背景

我正在玩JavaScript中的可选参数,以及如果未指定参数则分配默认值的想法.我的方法接受两个参数,后者我认为是可选的,如果未指定则默认为false.我的方法看起来像这样......

// selects the item, appropriately updating any siblings
// item to select [, toggle on / off]
this.selectItem = function(item, toggle)
{
    toggle = toggle && typeof toggle === 'boolean';
    if (toggle)
    {
       // ...
    }
}
Run Code Online (Sandbox Code Playgroud)

测试

此jsFiddle上运行一些测试后,使用以下主体进行默认值分配:

function checkParamIsBoolean(param)
{
    param = param && typeof param === 'boolean';
}

checkParamIsBoolean('me'); // boolean
checkParamIsBoolean([]); // boolean
checkParamIsBoolean(true); // boolean
checkParamIsBoolean(false); // boolean
checkParamIsBoolean(1 == 1); // boolean
checkParamIsBoolean(1); // …
Run Code Online (Sandbox Code Playgroud)

javascript

27
推荐指数
2
解决办法
2万
查看次数

使用'使用'而不是关闭WebResponse和StreamReader是否安全

目前

我为HttpWebRequest被调用实现了一个简单的帮助方法GetResponse(url).目前,我手动关闭WebResponseStreamReader读取结果之后.然后我就这样返回结果:

// construct the request
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";

// get the result
WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string result = reader.ReadToEnd();

// clean up and return the result
reader.Close();
response.Close();
return result;
Run Code Online (Sandbox Code Playgroud)

建议

在陈述中包含回报而不是关闭它们是否安全using ; 这会和.Close()es 有同样的效果吗?

// construct the request
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";

// get the result
using (WebResponse response = request.GetResponse())
{
    using …
Run Code Online (Sandbox Code Playgroud)

c# httpwebrequest streamreader

16
推荐指数
1
解决办法
5230
查看次数

jQuery Sortable - 取消并恢复不按预期工作

问题(问题的jsFiddle演示)

revert当与canceljQuery sortable中的方法结合使用时,我遇到了一些设置问题.取消方法,如jQuery Sortable文档中所述:

取消当前可排序的更改并将其恢复为当前排序开始之前的状态.在停止和接收回调函数中很有用.

这在stopreceive回调中都可以正常工作,但是如果我在可排序的连接列表中添加一个revert 持续时间,它就会开始变得有趣(参见jsFiddle here).

理想情况下,在取消时,revert可能根本不会发生,或者在更理想的世界中,它会优雅地恢复到它的原始位置.任何想法我怎么能得到revertcancel发挥好?

预期

  1. 从左侧列表拖动到右侧列表
  2. 删除项目
  3. 项目动画到原始位置- 或 -立即转移到原始位置

实际

  1. 从左侧列表拖动到右侧列表
  2. 删除项目
  3. 项目动画到位置,假设可排序成功
  4. 项目立即转移到原始位置,因为可排序被取消

澄清

revert如果成功,该属性将项目移动到项目将丢弃的位置,然后由于revertcancel方法之前发生,则立即移回到原始位置.有没有办法改变生命周期,所以如果cancel方法执行,revert不是,而是项目立即返回到它的原始位置?

javascript jquery jquery-ui

13
推荐指数
2
解决办法
1万
查看次数

我什么时候应该在IEnumerable <T>的上下文中使用.Count()和.Count

我知道这.Count()是LINQ中的扩展方法,从根本上它使用了.Count,所以我想知道,我Count()应该何时使用,何时应该使用.Count?是.Count()主要较好的保存,可查询的集合是尚未执行,因此不具有枚举了吗?我是否更安全,只是总是使用.Count()扩展方法,反之亦然?或者这是否完全取决于收藏?

任何建议或文章都非常感谢.

更新1

.Count()在LINQ中反编译扩展方法之后,如果是或者,它似乎正在使用该.Count 属性,这是大多数答案所建议的.现在我能看到的唯一真正的开销是额外的空值和类型检查,我认为这并不是很大,但如果性能至关重要,仍然可以产生少量差异.IEnumerable<T>ICollection<T>ICollection

这是.Count().NET 4.0中反编译的LINQ 扩展方法.

public static int Count<TSource>(this IEnumerable<TSource> source)
{
    if (source == null)
    {
        throw Error.ArgumentNull("source");
    }

    ICollection<TSource> collection = source as ICollection<TSource>;
    if (collection != null)
    {
        return collection.Count;
    }

    ICollection collection2 = source as ICollection;
    if (collection2 != null)
    {
        return collection2.Count;
    }

    int num = 0;
    checked
    {
        using …
Run Code Online (Sandbox Code Playgroud)

.net c# linq generics collections

9
推荐指数
1
解决办法
2269
查看次数

如何在ScriptManager中更改已注册脚本的顺序

背景

在我们的母版页中,我们ScriptManager看起来像这样

<asp:ScriptManager ID="ScriptManager" runat="server" EnablePartialRendering="False" EnableScriptLocalization="true">
    <Scripts>
        <asp:ScriptReference Path="~/js/master.js" />
        <asp:ScriptReference Path="~/js/jquery-min.js" />
    </Scripts>
</asp:ScriptManager>
Run Code Online (Sandbox Code Playgroud)

我通过执行以下操作在页面上的嵌套控件中注册其他脚本

protected override void OnPreRender(EventArgs e)
{
    ScriptManager.RegisterClientScriptInclude(this, this.GetType(), "jquery-ui", "~/js/jquery-ui.js");
    base.OnPreRender(e);
}
Run Code Online (Sandbox Code Playgroud)

预期

  1. master.js
  2. jQuery的min.js
  3. jquery-ui.js*

实际

  1. jquery-ui.js*
  2. master.js
  3. jQuery的min.js

有没有办法更改已加载已注册脚本的顺序?

- 要么 -

是否有可能至少确保在ScriptManager代码隐藏注册脚本之前注册在实际块中注册的脚本?

.net c# asp.net master-pages

8
推荐指数
1
解决办法
2812
查看次数

资源 FullCalendar 中的固定列宽

我正在使用支持资源视图的 FullCalendar 特殊版本。 http://tux.fi/~jarnok/fullcalendar-resourceviews/

我只有几个问题。它似乎会自动设置列宽,我认为应用这种地址可以:

.fc-widget-header
{
    width:100px;
}
.fc-widget-content
{
    width:100px;
}
.fc-resourceName
{
    background-color:aliceblue;
    width:150px;
}
Run Code Online (Sandbox Code Playgroud)

然后 html 写入单元格的宽度为 100px,但它仍在尝试自动适应所有内容。

我想要它做的是如果它太大,无法水平滚动,但我总是希望每个单元格正好是 100px。

我有这种滚动风格:

#calendar
{
    overflow-x:auto;
}
Run Code Online (Sandbox Code Playgroud)

但元素仍然根据日期显示为可变宽度。它看起来是这样的:

在此处输入代码

显然,我的每个列都不是我要求的 100px,资源列也不是我要求的 150px。我该怎么做才能获得想要的外观?

另一个问题是没有为新的一周定义课程。我真的很想以不同的颜色显示第 X 周的列。

对其中任何一个的任何见解都会非常有帮助。

谢谢

css fullcalendar

5
推荐指数
1
解决办法
2万
查看次数

效率:Func <T>,Vs的实例

最近我一直在尝试使用这门Func<T>课程,到目前为止我很喜欢它.然而我注意到越来越多的我开始使用它而不是实际使用的实例T,所以我想问; 使用Func<T>vs 的开销是T多少?我知道这是一个有点普遍的问题,T可以是任何东西,所以我想这个问题应该集中在,传递函数的开销是什么,而不是简单对象的实例?

为了论证,我们假设如下.

我们的模拟对象, T

public class Person
{
    private string _name = string.Empty;
    private int _age = 0;
    private bool _isMale = true;

    public Person(string name, int age, bool isMale)
    {
        this.Name = name;
        this.Age = age;
        this.IsMale = isMale;
    }

    public string Name
    {
        get { return this._name; }
        set { this._name = value; }
    }

    public int Age
    {
        get { return this._age; } …
Run Code Online (Sandbox Code Playgroud)

.net c# generics functional-programming

4
推荐指数
1
解决办法
283
查看次数

为什么.NET Web控件事件处理程序不通用?

我一直在想这一段时间; 但尤其是因为过去几周我一直更专注于前端开发.这可能听起来像一个广泛的问题,但希望有一个答案或理由:

为什么.NET Web控件事件处理程序不通用?

推理

我问的原因是由于强类型事件处理程序的精确和优雅.在我的项目中,无论何时需要,我倾向于使用.NET泛型EventHandler<T>委托,它自.NET 2.0以来一直存在; 如这里讨论.

public delegate void EventHandler<TArgs>(object sender, TArgs args) where TArgs : EventArgs
Run Code Online (Sandbox Code Playgroud)

对此进行扩展并为其定义类型也是相对简单的sender,就像这样.

public delegate void EventHandler<TSender, TArgs>(TSender sender, TArgs args) where TArgs : EventArgs
Run Code Online (Sandbox Code Playgroud)

每当使用.NET控件时,偶尔我会发现自己在代码隐藏而不是ASPX文件中绑定事件处理程序,然后object如果我需要进行任何额外的检查或更改,则必须将其转换为所需的类型.

现有

定义

public class Button : WebControl, IButtonControl, IPostBackEventHandler
{
    public event EventHandler Click;
}
Run Code Online (Sandbox Code Playgroud)

履行

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    this.MyButton.Click += new EventHandler(MyButton_Click);
}

protected void MyButton_Click(object sender, EventArgs e)
{
    // type cast and …
Run Code Online (Sandbox Code Playgroud)

.net c# generics

4
推荐指数
1
解决办法
210
查看次数

为什么这个正则表达式适用于JavaScript,而不是C#?

表达

var regex = new Regex(@"{([A-z]*)(([^]|:)((\\:)|[^:])*?)(([^]|:)((\\:)|[^:])*?)}");
Run Code Online (Sandbox Code Playgroud)

分解

表达式[粗略地]设计为使用以下格式在输入中查找标记:{name[:pattern[:format]]},其中patternformat是可选的.

{
  ([A-z]*) // name
  (([^]|:)((\\:)|[^:])*?) // regex pattern
  (([^]|:)((\\:)|[^:])*?) // format
}
Run Code Online (Sandbox Code Playgroud)

此外,表达式尝试忽略转义冒号,从而允许使用诸如的字符串 {Time:\d+\:\d+\:\d+:hh\:mm\:ss}

当测试RegExr.com,一切工作充分,试图在C#中相同的图案然而,当,输入匹配失败,为什么?

(对表达式进行一般改进的任何建议也非常受欢迎)

c# regex

4
推荐指数
1
解决办法
120
查看次数

将UTC日期时间转换为本地日期时间

我多次尝试将utc datetime转换为本地日期时间,但我失败了.我的utc日期时间格式是

3月8日星期五23:12:27 UTC + 0200 2013

我的JavaScript代码也是

var time = Date(param_time);//param_time is /Date(1362866006646)/
Run Code Online (Sandbox Code Playgroud)

然后时间是太阳3月10日00:21:54 UTC + 0200 2013我需要将datetime作为2008-01-28T20:24:17Z因为我将本地日期时间转换为漂亮的日期时间.

http://ejohn.org/files/pretty.js

我怎样才能做到这一点 ?我在stackoverflow上查看了很多问题,但是没有人能够正常工作.谢谢.

javascript jquery

2
推荐指数
1
解决办法
1万
查看次数

为什么`Random`默认不包含静态`Next`方法?

可能重复:
类System.Random ..为什么不静态?

"生成的随机数始终相等"之后,我想知道;

为什么Random类没有为Next可选的最小值和最大值公开静态方法?这可能听起来像一个愚蠢的问题,但从经验来看,我想生成一个随机数9次,而不必明确指定种子?我错过了一些明显的东西,这是有原因的吗?或者实际上有一种我正在解释的方法,我还没有发现?

.net c# random

0
推荐指数
1
解决办法
198
查看次数