标签: global-scope

如何在整个解决方案中声明具有全局范围的扩展

我正在尝试编写一个扩展名(实际上取自Case insensitive'Contains(string)')

它在进行字符串比较时补偿土耳其测试.扩展本身很简单:

public static bool Contains(this string source, string toCheck, StringComparison comp)
{
  return source.IndexOf(toCheck, comp) >= 0;
}
Run Code Online (Sandbox Code Playgroud)

现在关键是我正在试图弄清楚在哪里/如何包含这个,以便整个解决方案(包含多个项目,每个项目都有自己的命名空间和类),可以通过string.Contains轻松访问它.做class.string.Contains或其他方式.

假设有一个项目'Util',它包含在所有其他项目中,有些我可以将它放在Util中(没有类?),以便它可以作为string.Contains在整个解决方案中全局引用?

这甚至可能吗?如果是这样的话?

c# global-scope turkey-test

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

在没有全局范围的情况下使用Laravel触摸

概念问题: 在使用touches属性时,我有一个非常简单的问题,即自动更新依赖模型的时间戳; 它正确地这样做但也适用于全局范围.

有没有办法关闭此功能?或者专门要求自动 touches忽略全局范围?


具体示例:更新配料模型时,应触及所有相关配方.这样可以正常工作,除了我们globalScope根据区域设置分离配方,在应用触摸时也会使用它.


成分模型:

class Ingredient extends Model
{
    protected $touches = ['recipes'];

    public function recipes() {
        return $this->belongsToMany(Recipe::class);
    }

}
Run Code Online (Sandbox Code Playgroud)

食谱型号:

class Recipe extends Model
{
    protected static function boot()
    {
        parent::boot();
        static::addGlobalScope(new LocaleScope);
    }

    public function ingredients()
    {
        return $this->hasMany(Ingredient::class);
    }
}
Run Code Online (Sandbox Code Playgroud)

区域范围:

class LocaleScope implements Scope
{
    public function apply(Builder $builder, Model $model)
    {
        $locale = app(Locale::class);

        return $builder->where('locale', '=', $locale->getLocale());
    }

}
Run Code Online (Sandbox Code Playgroud)

php global-scope laravel

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

为什么这个变量引用非局部作用域不能解析?

这是一个寻找正整数aandb和 的最大公约数的例子a <= b。我从较小的a和一个一个的减号开始,检查它是否是两个数字的除数。

def gcdFinder(a, b):

    testerNum = a   

    def tester(a, b):
        if b % testerNum == 0 and a % testerNum == 0:
            return testerNum
        else:
            testerNum -= 1
            tester(a, b)

    return tester(a, b)

print(gcdFinder(9, 15))
Run Code Online (Sandbox Code Playgroud)

然后,我收到错误消息,

UnboundLocalError: local variable 'testerNum' referenced before assignment.

使用后global testerNum,它成功地3在Spyder控制台中显示了答案......

间谍的结果

但是在 pythontutor.com 中,它说NameError: name 'testerNum' is not defined链接)。

pythontutor的结果

Q1:在 Spyder 中,我认为这global testerNum是一个问题,因为 …

python scope global-scope python-3.x spyder

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

在Node.js和Browser中对'this'的不同处理

我在本地安装了Node.js v8.10.0.我写了一个简单的脚本来玩'this':

var x = 1;

var fn = function (a) {
     var x = a;

     console.log(`local x = ${x}`);
     console.log(`global x = ${this.x}`);
}

fn(10);
Run Code Online (Sandbox Code Playgroud)

当我通过Node.js执行脚本时,我得到以下结果:

local x = 10

global x = undefined
Run Code Online (Sandbox Code Playgroud)

当我在Chrome中执行脚本时,我得到以下结果:

local x = 10

global x = 1
Run Code Online (Sandbox Code Playgroud)

您能否向我解释一下,为什么Node.js在全局范围内没有看到x?

javascript this global-scope node.js

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

如何在Javascript中将文本附加到函数内的变量?

var err = '';

err += 'err a';
myString = 'Blah';
new Ajax.Request('/check_me.php',
    {
        method:'get',
        parameters: {
                'string': myString
            },
            evalScripts: true,
            onComplete: function(t){
                var response = t.responseText;
                if (response == false) {
                    //do nothing
                } else {
                    err += 'err b.\n';
                    //alert(err);
                }
            }
        });

err+= 'err c';

alert(err);
Run Code Online (Sandbox Code Playgroud)

在上面,它应警告"err a"+"err b"+"err c".但我只是得到"错误"+"错误".如果我尝试使用oncomplete警告(错误),那么我可以看到文本被附加到它之前的任何值.在这种情况下,"错误"+"错误b".如果我关闭此警告框,则最后一个警告框只显示a和c.

所以它是从全局变量中读取值而不是写入它.

如何使其工作,即将其设置为"b"?

谢谢

javascript prototypejs global-scope

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