我已经读过全局变量都不好的地方,应该使用替代方案.在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吗? …
在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)
我想从comment
表comment_key
中获得meta
表中具有特定value
(表中的value
列meta
)对应的注释.
例如,我想选择所有从该行comment
有一个表value
中1
的meta
表:
我期待这些结果:
key | value
=================
1 | foo
2 | bar
4 | barfoo
Run Code Online (Sandbox Code Playgroud)
而如果我选择所有从该行comment
有一个表value
中2
的 …
我有一个authService
实例化时订阅AngularFireAuth
的 ObservableauthState
并设置服务的内部(私有)属性authState
。
所以我可以在我的测试规范中authService
对服务的内部authState
进行单元测试,Reflect.get/set
以便我可以控制它的价值。
问题当然authService
是在实例化期间仍然订阅AngularFireAuth
's ObservableauthState
,我不想要,也不需要它。
我假设我需要模拟出AngularFireAuth其假货订阅,并不实际沟通火力地堡?单元测试的新手我不知道我应该如何做到这一点。
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) 我想向一些没有或几乎没有单元测试经验的同事介绍单元测试.我将首先介绍大约一个小时来解释这个概念并提供大量示例.我将跟进结对编程会话和代码审查.
在引导中应该关注哪些关键点?
我在正则表达式上很垃圾,真的!
我想要的是将包含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
!
谢谢你们!
我刚刚开始使用Grunt,并希望每次修改文件时使用[GitHub页面]运行grunt-contrib-watch
[GitHub页面] lint我的JavaScript(使用grunt-contrib-jshint
[GitHub页面])并运行grunt-nodemon
[GitHub页面],同时使用grunt-concurrent
[GitHub页面].
据我所知(我显然没有)我的Gruntfile应该:
concurrent
默认运行concurrent
运行 watch
watch
jshint
每次修改文件时都会运行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) 我正在尝试使用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) 这可能看起来很奇怪,但我实际上需要一个会超时的 PHP 脚本,即:执行 Apache 将超时脚本并触发 408 错误需要很长时间。
这样做的目的是演示使用自定义 408 错误页面捕获 408 以向数据库报告超时。
也许您建议有更好的方法来做到这一点?
如果我没记错的话,我认为无限循环不会像 Apache 500 那样工作。
编辑 - - - -
有人指出 408 是客户端错误,那么另外,如果脚本超时,Apache 会触发什么错误?
我想要一个实际的例子而不是一个综合的例子,header()
因为这是向客户推销的,他们最喜欢一个真实的例子。
谢谢!
我正在尝试测试具有构造函数的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) 继我之前的问题,关于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) unit-testing ×3
angular ×2
firebase ×2
jasmine ×2
javascript ×2
php ×2
regex ×2
.htaccess ×1
angularfire ×1
angularfire2 ×1
angularjs ×1
apache ×1
delimiter ×1
facebook ×1
gruntjs ×1
join ×1
jshint ×1
mysql ×1
node.js ×1
nodemon ×1
oauth ×1
preg-split ×1
select ×1
split ×1
sql ×1
timeout ×1
typescript ×1
watch ×1