小编Sey*_*don的帖子

为什么 JavaScript 的 parseInt(0.0000005) 打印“5”?

我读过一篇关于JavaScript parseInt 的文章,其中有这个问题:

parseInt(0.5);      // => 0
parseInt(0.05);     // => 0
parseInt(0.005);    // => 0
parseInt(0.0005);   // => 0
parseInt(0.00005);  // => 0
parseInt(0.000005); // => 0

parseInt(0.0000005); // => 5
Run Code Online (Sandbox Code Playgroud)

为什么会发生这种情况?

javascript type-conversion parseint node.js typescript

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

基于 vuejs v3.0 中的类组件的 vuejs 项目会发生什么?

我想将我的 vuejs 项目升级到基于类组件的打字稿,但我在 vue 问题的https://github.com/vuejs/rfcs/pull/17#issuecomment-494242121 中阅读:

更新:Class API 提案正在被删除。

那么现有的基于类组件的项目会发生什么?更重要的是,基于这篇文章: https://alligator.io/vuejs/using-typescript-with-vue/其说

由于我们没有使用类样式语法,因此您可以使用 as 关键字将数据声明为数据类型。

这种在 vue3.0 中使用 typescript 的方式安全吗?

typescript vue.js vue-class-components

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

如何正确重置 Vue Composition Api 的反应值

我想知道我应该如何在 vuejs 设置中重置反应?(我知道如果将其更改为 ref 并使用 view.value 将解决此问题,但使用反应式应该有一个答案)

在此处输入图片说明

javascript typescript vuejs3 vue-composition-api

10
推荐指数
2
解决办法
3289
查看次数

如何在 vuejs 中正确处理元素外部的点击?

是否有任何正确的解决方案来处理元素外部的点击?

有一些通用的解决方案,例如在没有 jquery 的情况下处理元素外部的点击

window.onload = function() {

    // For clicks inside the element
    document.getElementById('myElement').onclick = function(e) {
            // Make sure the event doesn't bubble from your element
        if (e) { e.stopPropagation(); } 
        else { window.event.cancelBubble = true; }
            // Place the code for this element here
        alert('this was a click inside');
    };

    // For clicks elsewhere on the page
    document.onclick = function() {
        alert('this was a click outside');
    };
};
Run Code Online (Sandbox Code Playgroud)

但问题是几乎所有项目在不同的组件中都有多个不同的弹出窗口,我应该处理它们的外部点击。

我应该如何在不使用全局 window.on 的情况下处理 click-outisde ?(我认为不可能将所有组件的外壳处理程序放入 window.on …

html javascript event-handling dom-events vue.js

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

如何扩展可以接受args的console.log

我一直在努力扩展console.log以制作一个彩色的,以保持堆栈跟踪到它被调用的位置。我尝试了一些解决方案,但最终达到了这一点:

export const colorizedLog = (
  text: string,
  status: keyof typeof ColorStatus = "dark",
  ...args: any
) =>
  console.log.bind(
    console,
    `%c ${text}`,
    `font-weight:bold; color: ${ColorStatus[status]}`,
    ...args
  );
Run Code Online (Sandbox Code Playgroud)

有了这个小binding,我们将能够保留堆栈跟踪,但这里的问题是我们必须在最后使用它并添加一个烦人的额外内容(),因为返回值是函数本身,它是 的结果bind

 colorizedLog(
    "Title:",
    "info",
    "This is a working example")();
Run Code Online (Sandbox Code Playgroud)

我阅读过的其他主要资源如下:

  1. 扩展 console.log
  2. 使用 babel-kent.c dodds 的宏
  3. Webpack 定义插件

const colorizedLog = (text, color= "#40a7e3", ...args) =>
  console.log.bind(
    console,
    `%c ${text}`,
    `font-weight:bold; color:${color}`,
    ...args
  );
colorizedLog("Title:", "#40a7e3", "This is a working example")();
Run Code Online (Sandbox Code Playgroud)

更新:堆栈跟踪 …

javascript bind javascript-debugger typescript console.log

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

将“vue-enterprise-boilerplate”转换为打字稿库的问题?

我正在尝试在vue-enterprise-boilerplate 中使用打字稿。但是在这个问题https://github.com/chrisvfritz/vue-enterprise-boilerplate/issues/118 中已经说过流程存在一些问题。

我尝试了其他解决方案来使其工作,例如使用“vue add @vue/typescript”,但没有明确说明我应该选择哪个配置。

1? Use class-style component syntax?(y/N)
2? Use Babel alongside TypeScript (required for modern mode, auto-detected polyfills, transpiling JSX)? (y/N)
3? Convert all .js files to .ts? (y/N)
4? Allow .js files to be compiled? (y/N)
Run Code Online (Sandbox Code Playgroud)

编号 3q,告诉我选择 babel 到现代,而在那期作者告诉我:

使用 @vue/cli-plugin-typescript 而不是 @vue/cli-plugin-babel(同时删除 .babelrc.js)。

我也这样做了,但是我遇到了一些关于无法找到某些模块的新错误,当我尝试修复它们时,webpack 配置出现了一些新错误......是否有人尝试过这些步骤并且有一个关于这个问题的逐步解决方案。

enterprise typescript vuejs2

6
推荐指数
0
解决办法
186
查看次数

Safe regex patterns from ReDos attack

I've recently faced with some redos attack issues.

Explain in simple steps:

Regex denial of services: it means the attacker can put some malicious/crafted inputs to bring your server down by making it impossible to stop to finding the correct pattern, so it takes your whole CPU, and finally causing internal server error.

e.g. if you have a pattern like ^((ab)*)+$, and the attacker put a malicious input like abababababababababababababababababa it will cause a catastrophic error: 在此输入图像描述 在此输入图像描述

The …

javascript regex security ddos pattern-matching

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

如何解决Vuejs组合API的Ref()类型错误?

我正在使用 vue 组合 API 并使用 ref 来表示对象数组,每个对象都有一个 value 属性,似乎打字稿将 ref 的值与每个数组内的属性值混淆了:

在此输入图像描述

正如您在上图中看到的,打字稿告诉我们 arr 的类型是一个包含名称和值的对象数组,但是当我使用 ref 时,它检测到错误的类型。即使我使用如下所示的转换,问题也无法解决:

const arr2 = ref(arr as { name: string; value: number }[]);
Run Code Online (Sandbox Code Playgroud)

但是当我将 value 属性更改为 value2 或其他值时,它将起作用: 在此输入图像描述

更新

这是我今天遇到的另一个问题:

在此输入图像描述

我想知道我应该如何解决这种类型错误?

typeerror typescript vue.js vuejs2 vue-composition-api

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

如何在打字稿中解构枚举值?

我在打字稿中有一个枚举,如下所示:

export enum XMPPElementName {
  state = "state",
  presence = "presence",
  iq = "iq",
  unreadCount = "uc",
  otherUserUnreadCount = "ouc",
  sequenceID = "si",
  lastSequenceID = "lsi",
  timeStamp = "t",
  body = "body",
  message = "message"
}
Run Code Online (Sandbox Code Playgroud)

并想要解构它的价值,我们如何在 Typescript 中做到这一点?

const { uc, ouc, msg, lsi, si, t, body } =  XMPPElementName; 
Run Code Online (Sandbox Code Playgroud)

更新

正如 @amadan提到的,我们可以Assigning to new variable names像 Mozilla 文档中所说的那样使用Destructuring_assignment,如下所示:

分配给新变量名称

属性可以从对象中解包并分配给与对象属性名称不同的变量。

const { uc, ouc, msg, lsi, si, t, body } =  XMPPElementName; …
Run Code Online (Sandbox Code Playgroud)

javascript enums object typescript destructure

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

如何使用扩展包更新 vscode 中的用户 settings.json?

我开发了一个vscode extension VSCodeWebDeveloperExperiencePack,现在我面临着其中一些之间的冲突,例如turbo consoledeploy,我在扩展页面上编写了配置手册,如下所示:

您可能想要使用的预定义设置:

  1. ctrl+shift+p
  2. 类型settings
  3. 单击Prefrences: Open settings (JSON)打开您的settings.json 文件
  4. 添加以下几行设置:
  "workbench.iconTheme": "material-icon-theme",
  "workbench.colorTheme": "Atom One Dark",
  "files.autoSave": "afterDelay",
  "editor.fontFamily": "Fira Code",
  "editor.fontLigatures": "",
  "editor.formatOnSave": true,
  "editor.defaultFormatter": "esbenp.prettier-vscode",
  "editor.fontSize": 16,
  "window.zoomLevel": 1,
  "sync.gist": "70a5fe700fe4e46aebdf678a5c1db398",
  "typescript.preferences.importModuleSpecifier": "non-relative",
  "local-history.exclude": [
    "**/.history/**",
    "**/.vscode/**",
    "**/node_modules/**",
    "**/typings/**",
    "**/out/**",
    "**/Code/User/**"
  ]
Run Code Online (Sandbox Code Playgroud)

另外,还有一些keybindings配置如下:

[
  {
    "key": "ctrl+shift+alt+l",
    "command": "bookmarks.jumpToNext",
    "when": "editorTextFocus"
  },
  {
    "key": "ctrl+alt+l",
    "command": "-bookmarks.jumpToNext",
    "when": "editorTextFocus"
  },
  {
    "key": "ctrl+shift+alt+k", …
Run Code Online (Sandbox Code Playgroud)

visual-studio-code vscode-extensions

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

如何使用正则表达式来匹配所有可能包含停用词列表的句子?

目标是找到在短语之间可能包含停用词列表的所有句子,to_match如下所示:

  • 许愿
  • 许个愿
  • 许愿
let stopword: string[]= ["of", "the", "a"];
let to_match : string = "make wish";
let text: string = "make wish wish make a wish wish wish make the a wish make";
Run Code Online (Sandbox Code Playgroud)

我只能make wish使用这个正则表达式进行匹配:

const regex = new RegExp(`(?:\\b)$to_match(?:\\b)`, "gi"); 
Run Code Online (Sandbox Code Playgroud)

我想知道是否可以做类似的事情

let to_match_splitted: string[] = to_match.split(" ");
const regex = `(?:\\b)${to_match_splitted[0]}\s(${any(stopword)}?)+\s${to_match_splited[1]}(?:\\b)`;
Run Code Online (Sandbox Code Playgroud)

any(stopword)停用词列表中的任何停用词相匹配。

to_match_splitted并且有一个正则表达式,无论列表中每个字符串之间有一个或多个停用词的长度如何,都可以工作。

javascript regex typescript

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