我想删除原始数组中的特定元素(即var a
).我filter()
那个数组并splice()
返回了新数组.但这对此代码中的原始数组没有影响.如何从原始数组中轻松删除这些元素?
var a = [{name:'tc_001'}, {name:'tc_002'}, {name:'tc_003'}]
var b = a.filter(function (e) {
return e.name === 'tc_001';
});
b.splice(0,1);
console.log(a);
console.log(b);
Run Code Online (Sandbox Code Playgroud) 我正在使用Babel
并Webpack
从中生成ES5
代码ES6
.有一些验证用于减少我在编码时所犯的错误.
class Logger {
/**
* @param {LogModel} info
* {LogTypes} type
* {String} message
* {Date} date
*/
static log(info) {
if(info instanceof LogModel)
throw new Error("not a instance of LogModel");
notify(info);
}
}
Run Code Online (Sandbox Code Playgroud)
在log
函数中,我验证参数是否是LogModel
类的实例.这只是为了防止错误.我不希望如果条件在生产中,因为太多,如果条件会减慢应用程序.是否可以使用验证和生产版本生成开发版本,而无需使用Babel
和Webpack
?
假设我有一个同步功能path.join()
.我想把它包装成一个Promise
因为我希望在catch()
块内处理异常.如果我包裹像下面,我不能在异常Promise
的.catch()
块.所以我必须用来if
检查返回值,如果它是一个错误,然后调用resolve,拒绝函数.还有其他解决方案吗?
var joinPaths = function(path1,path2) {
return new promise(function (resolve, reject) {
resolve(path.join(path1, path2));
});
};
Run Code Online (Sandbox Code Playgroud) 我想从左到右对齐Rectangle
s RowLayout
.在下面的代码示例中,两个Rectangle
s共享额外空间而不是一个接一个地堆叠 我Layout.alignment: Qt.AlignLeft
在RowLayout
关卡和Rectangle
关卡中使用过,但是这两种方式都没有改变视图.
Item {
RowLayout {
anchors.fill: parent
spacing: 2
Rectangle {
width: 100
Layout.fillHeight: true
Layout.alignment: Qt.AlignLeft
Text {
text: "Hello world"
}
}
Rectangle {
width: 100
Layout.fillHeight: true
Layout.alignment: Qt.AlignLeft
Text {
text: "Hello world"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
在下面的图像中,黑色边框表示RowLayout
红色边框是Rectangle
s.
在下面的应用程序中,我希望 Quill 编辑器填充页眉和页脚中的现有空间。我尝试将其设为 100%,但这会为整个页面添加一个滚动条。如何让 Quill 同时填充空间以适应屏幕尺寸。(如果高度降低编辑器高度应该降低)
var quill = new Quill('#editor', {
modules: {
toolbar: [
[{ header: [1, 2, false] }],
['bold', 'italic', 'underline'],
['image', 'code-block']
]
},
placeholder: 'Compose an epic...',
theme: 'snow' // or 'bubble'
});
Run Code Online (Sandbox Code Playgroud)
html,body {
margin: 0;
height: 100%;
}
#container {
height: 100%;
display: flex;
flex-direction: column;
justify-content: space-between;
}
#header {
height: 40px;
background: red;
}
#footer {
height: 40px;
background: red;
}
#editor-container {
height: 100%;
}
#editor {
height: 100%; …
Run Code Online (Sandbox Code Playgroud)我有3个Node Js
功能.我在这里要做的是,我想调用normalizeFilePath
并获取规范化路径,然后检查文件是否存在,normalizedFilePath
并且在所有这些之后,如果文件尚不存在则创建文件.这是使用promises(Bluebird)的第一天,我是新手Node JS
和Java Script
.下面的代码结构越来越复杂.当然,这根本不是一个好主意.
var createProjectFolder = function (projectName) {
};
var checkFileExistance = function (filePath) {
return new promise(function (resolve, reject) {
normalizeFilePath(filePath).then(function (normalizedFilePath) {
return fs.existSync(normalizedFilePath);
});
})
};
var normalizeFilePath = function (filePath) {
return new promise(function (resolve, reject) {
resolve(path.normalize(filePath));
});
};
Run Code Online (Sandbox Code Playgroud)
我如何管理实施该概念的承诺?
如果我在网页上打开JavaScript控制台并添加变量:
var abc = "Hello";
Run Code Online (Sandbox Code Playgroud)
Javascript将该属性附加到window
对象,因此我可以简单地通过window.abc
或直接访问该对象abc
console.log(window.abc)
console.log(abc)
Run Code Online (Sandbox Code Playgroud)
现在,如果我尝试通过该window
对象访问尚未定义的内容
console.log(window.notdefinedyet)
> undefined
Run Code Online (Sandbox Code Playgroud)
它只是返回未定义。所以如果我这样做
console.log(notdefinedyet)
Run Code Online (Sandbox Code Playgroud)
为什么这是一个错误而不仅仅是错误undefined
?
我想向这个项目添加单元测试。这是一个基本的 webpack + typescript 项目,将由另一个 Web 应用程序使用。单元测试应该在浏览器上运行。
我尝试过mocha
,但只是import 'mocha'
抛出编译错误。(我有测试文件,其中project_folder/test/test.ts
是 webpack 的条目。)
WARNING in ./node_modules/mocha/lib/mocha.js 219:20-37
Critical dependency: the request of a dependency is an expression
@ ./node_modules/mocha/browser-entry.js
@ ./test/test.ts
WARNING in ./node_modules/mocha/lib/mocha.js 227:24-70
Critical dependency: the request of a dependency is an expression
@ ./node_modules/mocha/browser-entry.js
@ ./test/test.ts
WARNING in ./node_modules/mocha/lib/mocha.js 277:24-35
Critical dependency: the request of a dependency is an expression
@ ./node_modules/mocha/browser-entry.js
@ ./test/test.ts
WARNING in ./node_modules/mocha/lib/mocha.js 327:35-48
Critical dependency: the request …
Run Code Online (Sandbox Code Playgroud) 我有axios
以下HttpClient
课程
export default class HttpClient {
constructor(baseUrl: string) {
const axiosInstance = axios.create({
validateStatus(status: number) {
return status === 200 || status === 201;
},
});
axiosInstance.interceptors.request.use((config) => {
if (AuthUtil.getAuthHeader()) config.headers = AuthUtil.getAuthHeader();
return config;
});
return new Proxy(this, {
get(_, prop) {
return (url: string, ...args: any) => {
url = baseUrl + url;
return Reflect.get(axiosInstance, prop)(url, ...args);
};
},
});
}
get<T = any, R = AxiosResponse<T>>(_url: string, _config?: AxiosRequestConfig): Promise<R> {
return …
Run Code Online (Sandbox Code Playgroud) 我想promise
通过创建promise
并传递它并让其他人将 附加then
到它来检查对象的可移植性。输出与我的预期相去甚远,我不明白它是如何执行的。
const wait = time => {
return new Promise((res, rej) => {
setTimeout(res, time);
});
};
const promise = wait(5000)
.then(() => console.log('1'));
promise
.then(() => console.log('2'))
.then(() => console.log('3'));
promise
.then(() => console.log('4'))
.then(() => console.log('5'));
Run Code Online (Sandbox Code Playgroud)
输出:
1
2
4
3
5
Run Code Online (Sandbox Code Playgroud)
我调试了代码,这是我观察到的。调用通过传递回调来wait(5000)
调用Promise
调用setTimeout
API的构造函数res
,并 Promise
返回对象。then
然后它开始以相同的顺序注册从 1 到 5 的所有s。5 秒后,它res
从Promise
调用承诺链的对象中调用。
在我看来,then
API 将所有的添加callbacks
到一个数组或类似的东西中,从 1 …
我已经创建hello.c
并Makefile
在/home/coder/workspace/test_module
目录中。我正在使用Arch Linux。我已经安装好了linux-headers
。
#include <linux/init.h>
#include <linux/module.h>
static int hello_init(void){
printk(KERN_ALERT, "hello, this is hello module");
return 0;
}
static void hello_exit(void){
printk(KERN_ALERT, "exiting the hello module");
}
module_init(hello_init);
module_exit(hello_exit);
Run Code Online (Sandbox Code Playgroud)
obj-m=hello.o
KDIR = /usr/lib/modules/$(shell uname -r)/build
all :
make -C $(KDIR) M=$(PWD) modules
clean :
rm -rf *.o *.ko *.mod.* *.symvers *.order
Run Code Online (Sandbox Code Playgroud)
[coder@coder test_module]$ make
make -C /usr/lib/modules/4.10.13-1-ARCH/build M=/home/coder/workspace/test_module modules
make[1]: Entering directory '/usr/lib/modules/4.10.13-1-ARCH/build'
CC [M] /home/coder/workspace/test_module/hello.o
In file included …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用减速器计算元素总数。
// map: Map<(i8, i8), u8>
// row: Vec<u8>
row.push(
*map
.values()
.reduce(|acc, curr| &(acc + curr))
.unwrap_or(&0u8)
);
Run Code Online (Sandbox Code Playgroud)
我收到以下编译错误。
error[E0515]: cannot return reference to temporary value
--> src/main.rs:173:46
|
173 | .reduce(|acc, curr| &(acc + curr))
| ^-------------
| ||
| |temporary value created here
| returns a reference to data owned by the current function
Run Code Online (Sandbox Code Playgroud)
这里,reduce
是期望值&u8
,但不能更改为引用,因为它是临时值。如何解决这个问题?