小编Tân*_*Tân的帖子

通过模型注释中的正则表达式验证 List<string>

我正在使用 asp.net mvc 5。我有List<string>这样的:

var animals = new List<string>
{
   "Dog",
   "Cat"
};
Run Code Online (Sandbox Code Playgroud)

animals只能包含 2 个值:DogCat。因此,如果值为Tiger或 ,则无效Lion

这是我用来验证的基本方法:

var regex = new Regex(@"Dog|Cat");
foreach (string animal in animals)
{
   if (!regex.IsMatch(animal))
   {
      // throw error message here...
   }
}
Run Code Online (Sandbox Code Playgroud)

现在,我想声明一个模型Animal来存储列表:

class Animal
{
   //[RegularExpression(@"Dog|Cat", ErrorMessage = "Invalid animal")]
   public List<string> Animals { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

并在某些行动中:

public ActionResult Add(Animal model)
{
   if (ModelState.IsValid)
   {
      // do …
Run Code Online (Sandbox Code Playgroud)

c# regex

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

动态T的通用方法

我正在做一个小例子来检查参数的类型是否有效.

class A
{
}

class B
{
}

class C
{
}

class D
{
    public void SomeMethod<T>(T t) where T : class
    {
        if (t is A)
        {
            A a = t as A;
        }
        else if (t is B)
        {
            B b = t as B;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,我可以打电话:

A a = new A();
SomeMethod<A>(a);

B b = new B();
SomeMethod<B>(b);
Run Code Online (Sandbox Code Playgroud)

现在,我想阻止将类传递CSomeMethod.我想要实现的目标:

C c = new C();
SomeMethod<C>(c); // error
Run Code Online (Sandbox Code Playgroud)

为此,我尝试过:

public …
Run Code Online (Sandbox Code Playgroud)

c# generics

3
推荐指数
1
解决办法
168
查看次数

_proto_构造函数的正常函数和箭头函数有何不同?

let anonymous = function () {
    return 'hello'
};

let f = () => 'world';

console.log(new anonymous['__proto__'].constructor());
console.log(new f['__proto__'].constructor());


console.log(anonymous());
Run Code Online (Sandbox Code Playgroud)

anonymous当我尝试创建它的新实例时,这两个函数都返回一个带有名称的函数.

我的问题:当我调用anonymous()函数时,为什么它不会触及第二个构造函数(箭头函数)?

javascript constructor arrow-functions

3
推荐指数
1
解决办法
50
查看次数

如何在javascript中对类,函数和函数属性进行分类?

我有3个这样的对象:

class Person {
    constructor(name, age) {
        this._name = name;
        this._age = age;
    }
}

let Add = function (a, b) {
    return a + b
};

let Math = {
    multiply: function (x, y) {
        return x * y
    }
};
Run Code Online (Sandbox Code Playgroud)

尽可能地see,Person是一个类,Add并且Math.multiply是函数.

我的问题:如何分类class,functions在这种情况下?

我的问题来自:Person看起来像一个函数,像这样:

let Person = function (name, age) {
    this._name = name;
    this._age = age;
};
Run Code Online (Sandbox Code Playgroud)

此外,如果我声明一个对象来获取新的Add函数实例,则没有问题:

let result = new …
Run Code Online (Sandbox Code Playgroud)

javascript

3
推荐指数
1
解决办法
88
查看次数

覆盖函数的toString函数

我想通过答案生成GUID字符串.

'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
    var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
    return v.toString(16);
});
Run Code Online (Sandbox Code Playgroud)

现在,我想把它付诸实践toString,比如:GUID.NewGuid().toString().

我试过(不工作):

let GUID = function () {};
GUID.NewGuid = function () {};

GUID.NewGuid.prototype.toString = function () {
    let guid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        let r = Math.random() * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
    });

    return guid;
};
Run Code Online (Sandbox Code Playgroud)

未捕获的TypeError:无法读取未定义的属性'toString' console.log(GUID.NewGuid().toString()); …

javascript

3
推荐指数
1
解决办法
117
查看次数

如何等待异步函数?

我的情况:

let waiting = function () {
  return new Promise(resolve => {
    console.log('awaiting...');
    
    setTimeout(function () {
      resolve();
    }, 1000)
  });
};

let waitingAsync = async function () {
  console.log('start...');

  await waiting();

  console.log('stop...');
};

waitingAsync();
console.log('done...');
Run Code Online (Sandbox Code Playgroud)

代码中有两件事我不明白:

首先:

await waiting();
Run Code Online (Sandbox Code Playgroud)

waiting是一个同步函数(因为它没有async关键字)。那么,为什么我可以等待一个同步函数呢?

第二:

为什么done...完成waitingAsync功能后不能等待消息?

主要问题:waitingAsync是一个异步函数,为什么await调用它时不需要关键字?只是waitingAsync()代替await waitingAsync().

如果我可以 await waitingAsync()done...消息将最后打印。

javascript asynchronous ecmascript-2017

3
推荐指数
1
解决办法
3639
查看次数

Math.min和Math.max超出了最大调用堆栈大小

我有js函数可以在2d数组中找到最小值和最大值,这在小数组上也可以,但是当我将它传递给大数组时,它会给我range error

超出最大呼叫堆栈大小。

我正在使用最新版本的Chrome。

function MaxMin2dray(arr, idx){
    return {
       min: Math.min.apply(null, arr.map(function (e) { return e[idx]})),
        max: Math.max.apply(null, arr.map(function (e) { return e[idx]}))
  }
}
Run Code Online (Sandbox Code Playgroud)

javascript arrays

3
推荐指数
3
解决办法
1920
查看次数

为Moment对象添加天数(momentjs)

我正试图找到一种方法来增加时间.我可以根据当前时间让它工作一会儿,但就是这样.这是我目前的代码:

let start = moment('2017-01-15');
console.log(moment().add(7, 'days'));
console.log(moment(start).add(7, 'days'));
Run Code Online (Sandbox Code Playgroud)

这就是得到的结果:

片刻{_isAMomentObject:true,_isUTC:false,_pf:Object,_locale:Locale,_d:Mon Mar 13 2017 12:21:00 GMT-0400(Eastern Daylight Time)...}

片刻{_isAMomentObject:true,_ i:"2017-01-15",_ f:"YYYY-MM-DD",_ isUTC:false,_pf:Object ...}

所以它适用于moment()但不是那样的.无论我到哪里,我都应该这样做,所以我不知道自己错过了什么.

javascript momentjs

3
推荐指数
1
解决办法
7208
查看次数

任务<TResult> .Result vs等待任务

我写了一个小例子来获取5方法的价值TestMethod,我有两种方法可以做到:

    static async Task<int> TestMethod()
    {
        await Task.Delay(0);
        return 5;
    }

    static async Task Caller()
    {
        Task<int> test = TestMethod();
        int i = await test;
        Console.WriteLine("i: " + i);

        int k = test.Result;
        Console.WriteLine("k: " + k);
    }
Run Code Online (Sandbox Code Playgroud)

输出:

我:5

k:5

所以,我的问题是:await test和之间的区别是什么test.Result?何时使用await test/ test.Result

c# task

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

键盘事件正在降低性能

我的例子:

$(document).on('keyup', '[contenteditable=true]', function (e) {
        
    let _this = $(this), text = _this.text();

    if (text.length === 1) {
        let span = $('<span>').text(text);
        _this.html(span);
    }

    console.log(_this.html());

});
Run Code Online (Sandbox Code Playgroud)
[contenteditable=true] {
  border: 1px solid #ccc;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<div contenteditable="true"></div>
Run Code Online (Sandbox Code Playgroud)

我的问题:如果我以正常速度在div中键入一些文本(超过1个字符),则代码可以正常工作。但是,当我尝试快速键入文本时,没有将<span>标签附加到div。

我该如何解决?

html javascript jquery keyup

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