小编Hug*_*are的帖子

正则表达式:是否有AND运算符?

显然,您可以使用|(管道?)来表示OR,但有没有办法表示AND

具体来说,我想匹配包含某个短语的所有文本的段落,但没有特定的顺序.

regex lookahead

656
推荐指数
14
解决办法
71万
查看次数

JavaScript中代码组织的常见最佳实践

随着像jQuery这样的JavaScript框架使客户端Web应用程序更丰富,更实用,我开始注意到一个问题......

你是如何保持这种组织的?

  • 将所有处理程序放在一个位置并为所有事件编写函数?
  • 创建函数/类来包装所有功能?
  • 写得像疯了似的,只希望它能做到最好?
  • 放弃并获得新的职业?

我提到了jQuery,但它实际上是任何JavaScript代码.我发现随着线条开始堆积起来,管理脚本文件或找到你要找的东西变得更加困难.我发现最大的问题可能是有很多方法可以做同样的事情,很难知道哪一个是目前普遍接受的最佳实践.

是否有关于保持.js文件与应用程序其余部分一样美观和整洁的最佳方法的一般建议?或者这仅仅是IDE的问题?那里有更好的选择吗?


编辑

这个问题旨在更多地关注代码组织而不是文件组织.有一些非常好的合并文件或分割内容的例子.

我的问题是:目前普遍接受的组织实际代码的最佳实践方法是什么?您的方式是什么,甚至是推荐的方式与页面元素交互并创建可互相冲突的可重用代码?

有些人列出了名称空间,这是一个好主意.还有什么其他方法,更具体地说是处理页面上的元素并保持代码整洁有序?

javascript architecture formatting jquery design-patterns

561
推荐指数
17
解决办法
5万
查看次数

C#:'is'关键字并检查Not

这是一个愚蠢的问题,但您可以使用此代码来检查某些内容是否属于特定类型...

if (child is IContainer) { //....
Run Code Online (Sandbox Code Playgroud)

是否有更优雅的方法来检查"NOT"实例?

if (!(child is IContainer)) { //A little ugly... silly, yes I know...

//these don't work :)
if (child !is IContainer) {
if (child isnt IContainer) { 
if (child aint IContainer) { 
if (child isnotafreaking IContainer) { 
Run Code Online (Sandbox Code Playgroud)

是的,是的......愚蠢的问题....

因为对代码的外观存在一些疑问,所以它只是在方法开始时的简单返回.

public void Update(DocumentPart part) {
    part.Update();
    if (!(DocumentPart is IContainer)) { return; }
    foreach(DocumentPart child in ((IContainer)part).Children) {
       //...etc...
Run Code Online (Sandbox Code Playgroud)

c# casting keyword

263
推荐指数
7
解决办法
8万
查看次数

枚举定义中的波浪号(〜)是多少?

我总是感到惊讶的是,即使在现在使用C#之后,我仍然设法找到我不知道的事情......

我试过在网上搜索这个,但在搜索中使用"〜"对我来说效果不好,我在MSDN上也找不到任何东西(不是说它不存在)

我最近看到了这段代码,代字号(〜)是什么意思?

/// <summary>
/// Enumerates the ways a customer may purchase goods.
/// </summary>
[Flags]
public enum PurchaseMethod
{   
    All = ~0,
    None =  0,
    Cash =  1,
    Check =  2,
    CreditCard =  4
}
Run Code Online (Sandbox Code Playgroud)

我看到它有点惊讶所以我试图编译它,它有效...但我仍然不知道它意味着什么/做了什么.任何帮助?

c# enums language-features enumeration

146
推荐指数
7
解决办法
3万
查看次数

jQuery和"有组织的代码"

我最近一直在努力理解组织jQuery代码的最佳方法.我之前问了另一个问题,我认为我不够具体(在这里的问题中找到).

我的问题是,你做一个应用程序越丰富,你的客户端就越快失控.考虑这种情况......

//Let's start some jQuery
$(function() {        
    var container = $("#inputContainer");

    //Okay let's list text fields that can be updated
    for(var i=0; i < 5; i++) {

        //okay let's add an event for when a field changes
        $("<input/>").change(function() {

            //okay something changed, let's update the server
            $.ajax({
                success:function(data) {

                    //Okay - no problem from the server... let's update
                    //the bindings on our input fields
                    $.each(container.children(), function(j,w) {

                        //YIKES!! We're deep in here now!!
                        $(w).unbind().change(function() {

                            //Then insanity …
Run Code Online (Sandbox Code Playgroud)

javascript jquery code-organization

51
推荐指数
4
解决办法
8620
查看次数

在ASP.net MVC中,控制器实例上的HttpContext为null

这可能不是使用控制器的正确方法,但我确实注意到了这个问题并且没有找到解决方法.

public JsonResult SomeControllerAction() {

    //The current method has the HttpContext just fine
    bool currentIsNotNull = (this.HttpContext == null); //which is false    

    //creating a new instance of another controller
    SomeOtherController controller = new SomeOtherController();
    bool isNull = (controller.HttpContext == null); // which is true

    //The actual HttpContext is fine in both
    bool notNull = (System.Web.HttpContext.Current == null); // which is false        

}
Run Code Online (Sandbox Code Playgroud)

我注意到Controller上的HttpContext不是你在System.Web.HttpContext.Current中找到的"实际"HttpContext.

有没有办法在Controller上手动填充HttpContextBase?或者更好的方法来创建Controller的实例?

asp.net-mvc httpcontext

28
推荐指数
3
解决办法
4万
查看次数

匿名类型 - 是否有任何distingushing特征?

有什么可以使用,以确定一个类型是否实际上是一个匿名类型?例如界面等?

目标是创建以下内容......

//defined like...
public static T Get<T>(this IAnonymous obj, string prop) {
    return (T)obj.GetType().GetProperty(prop).GetValue(obj, null);
}
//...

//And then used like...
var something = new { name = "John", age = 25 };
int age = something.Get<int>("age");
Run Code Online (Sandbox Code Playgroud)

或者这只是匿名类型的美丽?没有什么可以自我识别,因为它需要一个新的形状?

注意 - 我意识到你可以为对象类编写一个扩展方法,但在我看来,这看起来有点矫枉过正.

.net c# generics extension-methods anonymous-types

26
推荐指数
1
解决办法
3568
查看次数

使用Delegates的局部变量

显然不是看起来像它不会是一个最佳实践.有人可以解释为什么它不是最佳实践或如何工作?任何提供解释的书籍或文章将不胜感激.

//The constructor
public Page_Index() {

    //create a local value
    string currentValue = "This is the FIRST value";

    //use the local variable in a delegate that fires later
    this.Load += delegate(object sender, EventArgs e) {
        Response.Write(currentValue);
    };

    //change it again
    currentValue = "This is the MODIFIED value";

}
Run Code Online (Sandbox Code Playgroud)

输出的值是第二个值"已修改".编译器魔术的哪个部分使这个工作?这跟跟踪堆上的值并稍后再次检索它一样简单吗?

[编辑]:鉴于一些评论,改变原来的一些句子......

c# heap delegates closures

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

C#vs VB.NET - 处理null结构

我遇到了这个,并想知道是否有人可以解释为什么这在VB.NET中工作,我希望它会失败,就像在C#中一样

//The C# Version

struct Person {
    public string name;
}
...
Person someone = null; //Nope! Can't do that!!
Person? someoneElse = null; //No problem, just like expected
Run Code Online (Sandbox Code Playgroud)

但是在VB.NET中...

Structure Person
    Public name As String
End Structure
...
Dim someone As Person = Nothing 'Wha? this is okay?
Run Code Online (Sandbox Code Playgroud)

没有什么不同于null(Nothing!= null - LOL?),或者这只是处理两种语言之间相同情况的不同方式?

为什么或两者之间的处理方式不同,这使得一个可以在一个,而不是另一个?

[更新]

鉴于一些评论,我更多地搞砸了......如果你想在VB.NET中允许某些东西为null,那么好像你真的必须使用Nullable ...例如...

'This is false - It is still a person'
Dim someone As Person = Nothing
Dim isSomeoneNull As Boolean = someone.Equals(Nothing) …
Run Code Online (Sandbox Code Playgroud)

c# vb.net nothing

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

C#反射:在成员字段上查找属性

我可能会错误地问这个问题,但可以/如何在自己的课程中找到字段......例如......

public class HtmlPart {
  public void Render() {
    //this.GetType().GetCustomAttributes(typeof(OptionalAttribute), false);
  }
}

public class HtmlForm {
  private HtmlPart _FirstPart = new HtmlPart();      
  [Optional] //<-- how do I find that?
  private HtmlPart _SecondPart = new HtmlPart();
}
Run Code Online (Sandbox Code Playgroud)

或者我可能只是错误地执行此操作...如何调用方法然后检查应用于自身的属性?

此外,为了这个问题 - 我很好奇是否有可能在不知道/访问父类的情况下找到属性信息!

c# reflection attributes field

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