小编s1n*_*7ax的帖子

过滤并删除数组中的过滤元素

我想删除原始数组中的特定元素(即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)

javascript arrays node.js

14
推荐指数
2
解决办法
4万
查看次数

删除生产分发文件中的一些代码行?

我正在使用BabelWebpack从中生成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类的实例.这只是为了防止错误.我不希望如果条件在生产中,因为太多,如果条件会减慢应用程序.是否可以使用验证和生产版本生成开发版本,而无需使用BabelWebpack

javascript babel webpack

9
推荐指数
4
解决办法
4581
查看次数

将同步函数包装到promise中的最佳方法是什么

假设我有一个同步功能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)

javascript node.js promise

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

如何在RowLayout中对齐项目

我想从左到右对齐Rectangles RowLayout.在下面的代码示例中,两个Rectangles共享额外空间而不是一个接一个地堆叠 我Layout.alignment: Qt.AlignLeftRowLayout关卡和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红色边框是Rectangles.

实际

Acual布局

预期

预期的布局

qt qml qt5 qtquick2

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

使 QuillJS 编辑器高度 100%

在下面的应用程序中,我希望 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)

javascript css flexbox quill

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

使用Promises在Bluebird承诺库的另一个函数内调用函数

我有3个Node Js功能.我在这里要做的是,我想调用normalizeFilePath并获取规范化路径,然后检查文件是否存在,normalizedFilePath并且在所有这些之后,如果文件尚不存在则创建文件.这是使用promises(Bluebird)的第一天,我是新手Node JSJava 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 node.js

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

了解Javascript如何全局解析变量

如果我在网页上打开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

javascript

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

浏览器上的单元测试(Typescript + Webpack)

我想向这个项目添加单元测试。这是一个基本的 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)

unit-testing typescript webpack

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

如何模拟AxiosInstance?

我有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)

typescript jestjs axios ts-jest

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

附加不同 Promise 链时 JavaScript Promise 的执行顺序

我想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调用setTimeoutAPI的构造函数res,并 Promise返回对象。then然后它开始以相同的顺序注册从 1 到 5 的所有s。5 秒后,它resPromise调用承诺链的对象中调用。

在我看来,thenAPI 将所有的添加callbacks到一个数组或类似的东西中,从 1 …

javascript node.js promise

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

printk的Linux内核模块错误:格式参数过多

我已经创建hello.cMakefile/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)

c linux makefile kernel-module

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

迭代器reduce中无法返回对临时值错误的引用

我正在尝试使用减速器计算元素总数。

// 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,但不能更改为引用,因为它是临时值。如何解决这个问题?

rust

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