在这种情况发生之前,我在本机脚本项目上取得了很好的进展:
JS: EXCEPTION: Uncaught (in promise): ReferenceError: __assign is not defined
这是从这行代码冒出来的:
return [...state, { ...action.payload, success: false }];
这是我的 tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"noEmitHelpers": true,
"noEmitOnError": true
},
"exclude": [
"node_modules",
"platforms",
"**/*.aot.ts"
]
}
Run Code Online (Sandbox Code Playgroud)
Typescript似乎没有__assign在编译源中包含它的辅助函数 - 这是它们实现对象扩展语法的方式.你们中的任何一个好人都会知道为什么吗?
忽略这是一个不好的add功能.这是一个关于在TypeScript中使用带有扩展语法的数组解构的问题.
这就是我正在尝试的
const add = ([x,...xs]) => {
if (x === undefined)
return 0
else
return x + add(xs)
}
console.log(add([1,2,3])) //=> 6
Run Code Online (Sandbox Code Playgroud)
但我不知道如何添加TypeScript类型.我最好的猜测是做这样的事情(最直接的翻译)
const add = (xs: number[]): number => {
if (xs[0] === undefined)
return 0;
else
return xs[0] + add(xs.slice(1));
};
console.log(add([1,2,3])); // => 6
Run Code Online (Sandbox Code Playgroud)
这两种职能的工作,但在打字稿我失去解构阵列参数,函数体被junked了像一堆难看的东西的能力,并xs[0]与xs.slice(1)-即使我这些抽象到自己的职能,这是除了点.
是否可以在TypeScript中添加类型来解构扩展参数?
到目前为止我尝试过的
这样的东西适用于固定数组
// compiles
const add = ([x,y,z]: [number, number, number]): number => ...
Run Code Online (Sandbox Code Playgroud)
但当然我需要可变长度数组输入.我试过这个,但它没有编译
// does not compile
const add = ([x, ...xs]: …Run Code Online (Sandbox Code Playgroud) 我的 React 应用程序中有一个 HOC,这里是代码。
export function checkToken<Props>(WrappedComponent: new() => React.Component<Props, {}>) {
return (
class PrivateComponent extends React.Component<Props, LocalState> {
constructor() {
super();
this.state = {
isAuthed: false
}
}
async componentDidMount() {
let token = localStorage.getItem('dash-token');
let config = {
headers: { 'x-access-token': token }
}
let response = await axios.get(`${url}/users/auth/checkToken`, config)
if (!response.data.success) {
localStorage.removeItem('dash-token');
browserHistory.push('/login');
} else {
this.setState({isAuthed: true});
}
}
render() {
let renderThis;
if (this.state.isAuthed) {
renderThis = <WrappedComponent {...this.props} />
}
return ( …Run Code Online (Sandbox Code Playgroud) 我在JavaScript中创建了一个脚本,在自动浏览器测试期间注入到我们的Ext JS应用程序中.该脚本测量在网格中加载数据所花费的时间.
具体来说,脚本轮询每个网格,查看是否存在第一行或"无数据"消息,并且一旦所有网格满足此条件,脚本将记录Date.now()和performance.timing.fetchStart之间的值. ,并将其视为页面加载的时间.
此脚本或多或少地按预期工作,但与人体测量时间(Google秒表ftw)相比,此测试报告的时间始终比秒表测量的时间长约300毫秒.
我的问题是这些:
脚本如下:
function loadPoll() {
var i, duration,
dataRow = '.firstRow', noDataRow = '.noData',
grids = ['.grid1', '.grid2', '.grid3','.grid4', 'grid5', 'grid6', 'grid7'];
for (i = 0; i < grids.length; ++i) {
var data = grids[i] + ' ' + dataRow,
noData = grids[i] + ' ' + noDataRow;
if (!(document.querySelector(data) || document.querySelector(noData))) {
window.setTimeout(loadPoll, 100);
return;
}
}
duration = Date.now() - performance.timing.fetchStart;
window.loadTime = duration;
}
loadPoll();
Run Code Online (Sandbox Code Playgroud)
一些考虑:
虽然我知道人类的响应时间可能很慢,但我确信使用谷歌秒表的人为因素并未引入300毫秒的不一致性.
查看代码可能看起来多个元素的轮询可能导致300毫秒的不一致,但是当我将被监视的元素数量从7更改为1时,在报告的时间内仍然有300毫秒的剩余时间.自动化测试.
我们的自动化测试在Selenium和Protractor控制的框架中执行.
如果您能够提供任何见解,请提前致谢!
例子:
hash = {'Apple':2, 'Orange' :1 , 'Mango' : 2}
Run Code Online (Sandbox Code Playgroud)
这里最大的键是 Apple 和 Mango。我如何编写一个函数,同时给出 Apple 和 Mango 作为答案。
我尝试过这样的事情:
Object.keys(hash).reduce(function(a, b){ return hash[a] > hash[b] ? a : b });
Run Code Online (Sandbox Code Playgroud)
但这只给出了苹果作为答案。
为了练习我对 Javascript 对象的知识,我编写了一个非常简单的程序:
const male = document.querySelector('.male');
const female = document.querySelector('.female');
/* Person constructor */
function Person (gender) {
this.gender = gender;
}
Person.prototype.bio = function() {
alert('This person is ' + this.gender + '.');
};
/* Create person */
male.addEventListener('click', function() {
let male1 = new Person('male');
return male1;
});
female.addEventListener('click', function() {
let female1 = new person('female');
return female1;
});
Run Code Online (Sandbox Code Playgroud)
但是,我遇到了范围问题:我无法在全局范围内调用male1或female1对象。跑步
male1;
Run Code Online (Sandbox Code Playgroud)
在浏览器控制台中返回以下错误:
ReferenceError: male1 is not defined
Run Code Online (Sandbox Code Playgroud)
如何正确返回新对象male1和female1,以便我可以在全局范围内访问它们?
在编写异步生成器函数时,我注意到以下构造会产生SyntaxError:
async function * foo() {
await yield bar; // Can not use 'yield' as identifier inside a generator
}
Run Code Online (Sandbox Code Playgroud)
尽管颠倒上下文关键字的顺序是完全可以接受的:
async function * foo() {
yield await bar; // OK
}
Run Code Online (Sandbox Code Playgroud)
仔细阅读错误后,我能够通过将 括在括号UnaryExpression内来纠正语法AwaitExpression,以避免将令牌解析yield为标识符而不是上下文关键字:
async function * foo() {
await (yield bar); // OK
}
Run Code Online (Sandbox Code Playgroud)
但这引出了一个问题,ECMAScript 2018中涉及哪些特定的静态语义,导致yield在这种情况下被解析为标识符,同时await不需要特殊处理?
我已经使用 hooks 一段时间了,但我从来没有完全理解为什么 React 强迫我在 useEffect 上包含一些我不想要的依赖项。
\n我理解 useEffect 挂钩的“依赖关系”的方式
\n添加您想要在更改时“监听”的值并触发您的效果。这与简单的效果完美配合,例如:
\nimport React, {useEffect, useState} from "react";\n\ninterface Props {\n\xc2\xa0 \xc2\xa0 id: string\n}\n\nconst SimpleComponent = (props: Props) => {\n\xc2\xa0 \xc2\xa0 const {id} = props;\n\xc2\xa0 \xc2\xa0 const [response, setResponse] = useState<object>();\n\xc2\xa0 \xc2\xa0 \n\xc2\xa0 \xc2\xa0 useEffect(() => {\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 fetch(`https://myexample/${id}`)\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 .then(response => setResponse(response))\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 .catch(() => console.log("An error occurs!"))\n\xc2\xa0 \xc2\xa0 }, [id])\n\xc2\xa0 \xc2\xa0 \n\xc2\xa0 \xc2\xa0 return <div/>\n};\nRun Code Online (Sandbox Code Playgroud)\n然而,还有一些其他情况并不像上面的例子那么简单。在这个例子中我们希望仅当 …
我在控制台中执行类似下面的代码
function add(a,b){return a+b;};
const obj = {...add};
Run Code Online (Sandbox Code Playgroud)
令我惊讶的是它没有抛出错误.也没有
const obj = {...123};
Run Code Online (Sandbox Code Playgroud)
扩展语法应该只适用于可迭代的实体,如对象,数组,字符串,映射,集合等.那么为什么在使用不可迭代的实体时它不会抛出错误?或者我在这里遗漏了什么?
原问题:
正数在一个补码和二进制补码中具有相同的表示。假设它的表示被解释为二进制补码,并且它的加法逆是确定的。现在这个表示被解释为一个补码,并且确定了加法逆。无论是解释为一的补码还是二的补码,结果都是一样的,因为它是一个正数。这个结果和原来的数字有什么关系?
我不知道他说的“这个结果和原来的数字有什么关系”是什么意思。我想我明白他要我们做的过程:
首先取二进制(0101),然后取二进制补码(1011),然后取二进制补码的二进制补码(0100)。接下来是什么?
javascript ×7
typescript ×3
reactjs ×2
c ×1
ecmascript-6 ×1
extjs ×1
generator ×1
key ×1
nativescript ×1
object ×1
performance ×1
react-hooks ×1
reduce ×1
scope ×1
syntax ×1
testing ×1
use-effect ×1