我正在阅读这本名为 Eloquent JavaScript 的好书,但我对这个例子中“绑定”这个词的使用感到困惑:
通过在属性名称周围使用方括号,可以在对象表达式和类中包含符号属性。这会导致属性名称被评估,就像方括号属性访问符号一样,它允许我们引用一个包含符号的绑定。
let stringObject = {
[toStringSymbol]() { return "a jute rope"; }
};
console.log(stringObject[toStringSymbol]());
// ? a jute rope
Run Code Online (Sandbox Code Playgroud)
据我了解(到目前为止在我的 JS 旅程中),“绑定”与指定this函数在哪个或对象上下文中运行有关。看这里。. 绑定可能与上下文有关。这就是为什么我们有.bind().
但是在这个例子中,我们绑定了其他东西(一个键是符号的方法)。是否binding只是意味着将属性(原语或方法)附加到对象上?
我刚刚在Mozilla网站上遇到过这段代码,虽然对我看起来很破旧,但我可能不熟悉它的用法:
for (; k < len; k++)
{
if (k in t && t[k] === searchElement)
return k;
}
Run Code Online (Sandbox Code Playgroud)
分号在循环开始时如何工作?
完整的代码在这里.
我一直在学习 React 课程。下一步已经使用,npm run eject以便可以使用 css 模块。
由于弹出我不能使用 npm start。页面无法编译。出现一长串 linting 警告(实际上似乎来自 webpack 配置文件)。
我为这些文件和其他文件创建了一个 .eslintignore 文件:
./reactNotes.jsx
./nodeTest.js
./config
./scripts
Run Code Online (Sandbox Code Playgroud)
但是我的代码编辑器(vs 代码)或 webpack 似乎都没有注意到这个文件。eslint 是全局安装的。
我还研究了 eslint 的 webpack 配置,其中包含诸如 exclude 之类的选项:
{
test: /\.(js|jsx|mjs)$/,
enforce: 'pre',
use: [
{
options: {
formatter: eslintFormatter,
eslintPath: require.resolve('eslint'),
},
loader: require.resolve('eslint-loader'),
},
],
include: paths.appSrc,
exclude: /config/
}
Run Code Online (Sandbox Code Playgroud)
VS Code 在我的用户设置中启用了 eslint。
如何设置 webpack(甚至可能是 vs 代码)来忽略目录和文件?
更新:这里是.eslintrc:
{
"parser": "babel-eslint",
"extends": "react-app",
"plugins": [
"react",
"jsx-a11y",
"import"
],
"rules": …Run Code Online (Sandbox Code Playgroud) 我在Typescript类方法声明中有一个错误,但是我不明白该错误消息与该bug的关系。
消息似乎是在说'this'是type any,但是我们在一个类定义中,所以我认为'this'确实很清楚。
有人可以解释一下错误消息与错误的关系吗?
原始方法:
calcSize = function() {
return this.width * this.length; // Error on this line
};
// Error text: 'this' implicitly has type 'any' because it does not
//have a type annotation.ts(2683)
//app.ts(39, 16): An outer value of 'this' is shadowed by this container.
Run Code Online (Sandbox Code Playgroud)
固定:
calcSize() {
return this.width * this.length;
};
Run Code Online (Sandbox Code Playgroud)
完整上下文(固定):
class BaseObject {
constructor(
public width: number = 0,
public length: number = 0
) {}
};
class Rectangle extends BaseObject {
constructor(public …Run Code Online (Sandbox Code Playgroud) 我正在使用 Spectron 测试一个 Electron/React 应用程序,它使用WebdriverIO 命令 API。我想测试一些组件的属性,但我想确保我只在组件重新加载后测试它们。
正常的 WebDriverIO 等待命令,如waitForText()或waitForExist(),等待一些更改,但我需要等到组件重绘发生。例如,组件在重绘之前已经存在或已经有一些文本。这意味着对文本的任何测试都将在重绘之前进行,我不会测试新文本。有没有等待重绘的通用方法?
我正在尝试将基于 React 的计时器示例转换为 Typescript。
这个类看起来有点像这样:
export class Container extends
Component<IProps, IState> {
private timerID: NodeJS.Timeout | undefined; // stops a warning in DidMount
constructor(props: IRevovlingContainerProps) {
super(props);
this.state = { date: new Date() } as any;
}
componentDidMount() {
this.timerID = setInterval(
() => this.tick(),
1000
);
}
componentWillUnmount() {
clearInterval(this.timerID); // error here on this.timerID
}
// ...
Run Code Online (Sandbox Code Playgroud)
在componentWillUnmount() this.timerID有一个错误:
[ts] 'Timeout | 类型的参数 undefined' 不能分配给类型为 'number | 的参数 不明确的'。“超时”类型不能分配给“数字”类型。[2345]
我检查过index.d.ts,它声明clearInterval(): …
看完这篇关于展平数组的帖子后,我注意到没有人使用过数组方法forEach.我试了一下但失败了,只收回了一个空数组:
let arrays = [[1, 2, 3], [4, 5], [6]];
let result = [];
arrays.forEach( (element) => {
result.concat(element)
})
console.log(result) //[]
Run Code Online (Sandbox Code Playgroud)
我哪里做错了?
我知道在 C++ 中,我们可以有一个指向常量值的常量指针:
const int value{ 5 };
const int* const ptr{ &value };
Run Code Online (Sandbox Code Playgroud)
这意味着:
ptr无法更改。ptr来改变value这样的指针什么时候有用?
javascript ×3
reactjs ×2
typescript ×2
arrays ×1
binding ×1
c++ ×1
class ×1
concat ×1
e2e-testing ×1
eslint ×1
for-loop ×1
foreach ×1
oop ×1
pointers ×1
spectron ×1
syntax ×1
timer ×1
webdriver-io ×1
webpack ×1