小编Jon*_*tes的帖子

我听说全球变量很糟糕,我应该使用哪种替代解决方案?

我已经读过全局变量都不好的地方,应该使用替代方案.在Javascript中,我应该选择什么解决方案.

我正在考虑一个函数,当有两个arguments(function globalVariables(Variable,Value))时,看看Variable是否存在于一个本地数组中,并且它是否将它的值设置为Value,else,Variable并被Value追加.如果在没有arguments(function globalVariables())的情况下调用该函数,则返回该数组.也许如果仅使用一个参数(function globalVariables(Variable))触发该函数,它将返回Variable数组中的值.

你怎么看?我想听听你使用全局变量的替代解决方案和论据.

你会怎么用 globalVariables();

function append(){
    globalVariables("variable1","value1"); //globalVariables() would append variable1 to it's local array.
};

function retrieve(){
    var localVariable1 = globalVariables("variable1"); //globalVariables() would return "value1".
};

function retrieveAll(){
    var localVariable1 = globalVariables(); //globalVariables() would return the globalVariable()'s entire, local [persistently stored between calls] array.
};

function set(){
    globalVariables("variable1","value2"); //globalVariables() would set variable1 to "value2".
};
Run Code Online (Sandbox Code Playgroud)

这是Singleton Pattern BTW吗? …

javascript global-variables

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

从表中选择行,其中具有相同id的另一个表中的行在另一列中具有特定值

在MySQL中:

如果我们有两个表:

comments
key    |    value
=================
1      |    foo
2      |    bar
3      |    foobar
4      |    barfoo
Run Code Online (Sandbox Code Playgroud)

和:

meta
comment_key    |    value
=========================
1              |    1
2              |    1
3              |    2
4              |    1
Run Code Online (Sandbox Code Playgroud)

我想从commentcomment_key中获得meta表中具有特定value(表中的valuemeta)对应的注释.

例如,我想选择所有从该行comment有一个表value1meta表:

我期待这些结果:

key    |    value
=================
1      |    foo
2      |    bar
4      |    barfoo
Run Code Online (Sandbox Code Playgroud)

而如果我选择所有从该行comment有一个表value2的 …

mysql sql select join

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

在对 Angular 服务进行单元测试时模拟 AngularFireAuth

我有一个authService实例化时订阅AngularFireAuth的 ObservableauthState并设置服务的内部(私有)属性authState

所以我可以在我的测试规范中authService对服务的内部authState进行单元测试,Reflect.get/set以便我可以控制它的价值。

问题当然authService是在实例化期间仍然订阅AngularFireAuth's ObservableauthState,我不想要,也不需要它。

假设我需要模拟出AngularFireAuth其假货订阅,并不实际沟通火力地堡?单元测试的新手我不知道我应该如何做到这一点。

auth.service.ts

import { Injectable } from '@angular/core';

import { AngularFireAuth } from 'angularfire2/auth';
import * as firebase from 'firebase/app';
import { Observable } from 'rxjs/Rx';

@Injectable()
export class AuthService {
  private authState: firebase.User;

  constructor(private afAuth: AngularFireAuth) { this.init(); }

  private init(): void {
    this.afAuth.authState.subscribe((authState) => {
      if (authState === null) {
        this.afAuth.auth.signInAnonymously() …
Run Code Online (Sandbox Code Playgroud)

unit-testing jasmine firebase angularfire2 angular

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

解释单元测试的关键点是什么?

我想向一些没有或几乎没有单元测试经验的同事介绍单元测试.我将首先介绍大约一个小时来解释这个概念并提供大量示例.我将跟进结对编程会话和代码审查.

在引导中应该关注哪些关键点?

unit-testing

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

.split("1px")到Javascript中的["1px",1,"px"]

我在正则表达式上很垃圾,真的!

我想要的是将包含CSS属性值的字符串拆分为数组[string,value,unit].

例如:如果我提供了.split()方法,1px则返回["1px",1,"px"].如果我要供应,同样,10%它会回来["10%",10,"%"].

可以这样做吗?

我感谢你的帮助!

更新:["1.5em",1.5,"em"]如果1.5em提供的话,我也希望它返回.但是,如果可能的话,如果提供的话仍然返回null yellow.不幸的是/^([0-9]*\.?[0-9]*)(.*)/提供yellow将返回y,,y!

谢谢你们!

javascript regex split

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

与Grunt同时运行`watch`&`nodemon`

我刚刚开始使用Grunt,并希望每次修改文件时使用[GitHub页面]运行grunt-contrib-watch [GitHub页面] lint我的JavaScript(使用grunt-contrib-jshint [GitHub页面])并运行grunt-nodemon [GitHub页面],同时使用grunt-concurrent [GitHub页面].

据我所知(我显然没有)我的Gruntfile应该:

  1. concurrent默认运行
  2. concurrent 运行 watch
  3. watchjshint每次修改文件时都会运行

Gruntfile.js

module.exports = function (grunt) {
    grunt.initConfig({
        concurrent: {
            dev: [
                'watch'
            ],
            options: {
                logConcurrentOutput: true
            }
        },
        jshint: {
            server: [
                '**/*.js',
                '!node_modules/**/*.js'
            ],
            options: {
                node: true
            }
        },
        watch: {
            all: [
                '**/*/.js',
                '!node_modules/**/*.js'
            ],
            tasks: [
                'jshint'
            ]
        }
    });

    grunt.loadNpmTasks('grunt-concurrent');
    grunt.loadNpmTasks('grunt-contrib-jshint');
    grunt.loadNpmTasks('grunt-contrib-watch');

    grunt.registerTask('default', [
        'concurrent:dev'/*,
        'jshint',
        'watch'*/
    ]);
};

    grunt.loadNpmTasks('grunt-concurrent'); …
Run Code Online (Sandbox Code Playgroud)

watch node.js jshint gruntjs nodemon

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

来自Facebook的$ authWithOAuthRedirect后,Firebase $ onAuth的authData错误

我正在尝试使用Facebook登录验证我的Firebase(Angularfire)应用程序的用户.

当我使用弹出窗口进行身份验证时,一切都按预期工作,但是为了支持尽可能多的浏览器(iOS上的Chrome不支持弹出窗口,例如)我想要使用重定向($authWithOAuthRedirect)进行身份验证.

我已经确认我在Facebook上的设置是正确的(我的应用程序ID和秘密,例如)但是当我通过重定向进行Facebook身份验证后重定向回我的应用程序,$onAuth但是我没有我的Facebook authData.

相反,我有匿名authData.有点背景; 所有用户如果未经过其他身份验证,则匿名进行身份验证(例如,使用Facebook).

我看不出为什么会这样 - 用户现在应该通过Facebook验证,并拥有Facebook authData.

在某些情况下,我的代码的例外情况如下:

用户单击登录按钮时触发

function logIn () {
    firebaseAuth
        .$authWithOAuthRedirect('facebook', function (error) {
            if (error) {
                console.log(error);
            }
        });
}
Run Code Online (Sandbox Code Playgroud)

$onAuth(在我的Angular应用程序中run)

function run ($rootScope, firebaseAuth, sessionStore) {
    $rootScope
        .$on('$routeChangeError', function (event, next, prev, error) {
            if (error === 'AUTH_REQUIRED') {
                console.log(error);
            }
        });

    $rootScope
        .$on('$routeChangeSuccess', function (event, current, prev) {
            $rootScope.title = current.$$route.title;
        });

    firebaseAuth
        .$onAuth(onAuth);

    function onAuth (authData) {
        console.log(authData); …
Run Code Online (Sandbox Code Playgroud)

facebook oauth angularjs firebase angularfire

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

有意使 PHP 脚本超时

这可能看起来很奇怪,但我实际上需要一个会超时的 PHP 脚本,即:执行 Apache 将超时脚本并触发 408 错误需要很长时间。

这样做的目的是演示使用自定义 408 错误页面捕获 408 以向数据库报告超时。

也许您建议有更好的方法来做到这一点?

如果我没记错的话,我认为无限循环不会像 Apache 500 那样工作。

编辑 - - - -

有人指出 408 是客户端错误,那么另外,如果脚本超时,Apache 会触发什么错误?

我想要一个实际的例子而不是一个综合的例子,header()因为这是向客户推销的,他们最喜欢一个真实的例子。

谢谢!

php apache .htaccess timeout

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

测试在Jasmine中具有构造函数方法的Angular2 Pipe

我正在尝试测试具有构造函数的Angular Pipe的第一个障碍。

我的管道如下:

反向管道

import {
  IterableDiffer,
  IterableDiffers,
  Pipe,
  PipeTransform
} from '@angular/core';

@Pipe({
  name: 'reverse',
  pure: false
})
export class ReversePipe implements PipeTransform {

  private cached: Array<any>;
  private differ: IterableDiffer<Array<any>>;

  constructor(private differs: IterableDiffers) {
    this.differ = this.differs.find([]).create(null);
  }

  transform(array: Array<any>): Array<any> {
    // TODO: Throw an error if `array` isn't an Array
    if (Array.isArray(array) === false) return [];

    const changes = this.differ.diff(array);

    if (changes) this.cached = array.slice().reverse();

    return this.cached;
  }
}
Run Code Online (Sandbox Code Playgroud)

我相信通过几本教程,使用IterableDiffer效率是正确的。但这不是这个问题的主题。

我认为需要构造函数的事实是此简单测试失败的根源:

反向管道规格

import { ReversePipe …
Run Code Online (Sandbox Code Playgroud)

unit-testing jasmine typescript karma-jasmine angular

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

PHP preg_split有两个分隔符,除非分隔符在引号内

继我之前的问题,关于preg_split哪个答案超级快,多亏了尼克; 当分隔符在引号内时,我真的想将场景扩展为不拆分字符串.例如:

如果我有字符串foo = bar AND bar=foo OR foobar="foo bar",我希望在每个空格或=字符上拆分sting 但=在返回的数组中包含字符(当前效果很好),但我不想拆分字符串中的任何一个分隔符引号.

到目前为止我有这个:

<!doctype html>
<?php

$string = 'foo = bar AND bar=foo';

$array = preg_split('/ +|(=)/', $string, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);

?>
<pre>
<?php

print_r($array);

?>
</pre>
Run Code Online (Sandbox Code Playgroud)

哪个让我:

Array
(
    [0] => foo
    [1] => =
    [2] => bar
    [3] => AND
    [4] => bar
    [5] => =
    [6] => foo
)
Run Code Online (Sandbox Code Playgroud)

但是,如果我将字符串更改为:

$string = 'foo = bar AND bar=foo OR foobar …
Run Code Online (Sandbox Code Playgroud)

php regex delimiter preg-split

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