我是Javascript语言的新手,最近我开始研究js原型,并对以下代码中的一些奇怪输出感到困惑:
Array.prototype.print = function() {
console.log(this)
}
[1, 2, 3, 4].print();
Run Code Online (Sandbox Code Playgroud)
谁能告诉我它为什么会回来
无法读取undefined'属性'print'
如果我声明var array = [1, 2, 3, 4]
然后调用print函数array.print()
,它工作正常,所以我很困惑,那有什么不同?
Array.prototype.print = function() {
console.log(this)
}
var array = [1, 2, 3, 4]
array.print()
Run Code Online (Sandbox Code Playgroud)
从https://github.com/Microsoft/TypeScript/pull/3622:
超类型折叠:如果 B 是 A 的超类型,则 A & B 等价于 A。
然而:
type a = string & any; // Resolves to any, not string!?
Run Code Online (Sandbox Code Playgroud)
这个交集解析为任何。'any' 不是字符串的超类型吗?那么由于超类型折叠,这个交集不应该只是字符串吗?我错过了什么?
这里的用例是这样的:
type PropertyMap = {
prop1: {
name: "somename";
required: any;
};
prop2: {
name: "someothername";
required: never;
}
}
type RequiredOnly = {
[P in keyof PropertyMap]: PropertyMap[P] & PropertyMap[P]["required"]
}
// RequiredOnly["prop2"] correctly inferred to be never, but we've
// lost the type info on prop1, since it is now …
Run Code Online (Sandbox Code Playgroud) 我试图使用正则表达式来验证十进制值.我写下面的正则表达式,但它不允许第一个小数点值为.5或.6或.1
常规Exp:/^\d[0-9]{0,13}(\.\d{1,2})?$/
规则:
示例 - 有效输入
示例 - 输入无效
const valid = [
"0",
"0.5",
"1.55",
".5",
"1234567890123",
"1234567890123.5",
"1234567890123.00",
];
const invalid = [
".",
".0",
"1.234",
"5.",
"12345678901234",
"12345678901234.56",
];
const rgx = /^\d[0-9]{0,13}(\.\d{1,2})?$/
console.log("Checking valid strings (should all be true):");
valid.forEach(str => console.log(rgx.test(str), str));
console.log("\nChecking invalid strings (should all be false):"); …
Run Code Online (Sandbox Code Playgroud)我一直在尝试使用异步生成器来尝试制作一个“承诺排序”生成器,该生成器接受一系列承诺并按照它们解决或拒绝的顺序逐个产生承诺。所以像这样:
async function* orderProms(prom_arr) {
// Make a copy so the splices don't mess it up.
const proms = [...prom_arr];
while (proms.length) {
// Tag each promise with it's index, so that we can remove it for the next loop.
const {prom, index} = await Promise.race(proms.map((prom, index) => prom.then(
() => ({prom, index}),
() => ({prom, index})
)));
proms.splice(index, 1);
yield prom;
}
}
Run Code Online (Sandbox Code Playgroud)
想要像这样使用这个生成器:
const resAfter = (val, delay) => new Promise(res => setTimeout(() => res(val), delay));
const rejAfter …
Run Code Online (Sandbox Code Playgroud) My understanding is that for...in
loops are designed to iterate over objects in Javascript. See this post and this post.
Take the following example. This returns 'Uncaught TypeError: items is not iterable' in my console.
var text = {
name: "Coptic",
ranges: [[994, 1008], [11392, 11508], [11513, 11520]],
direction: "ltr",
year: -200,
living: false,
link: "https://en.wikipedia.org/wiki/Coptic_alphabet"
};
function dominantDirection(items) {
for (let item of items) {
if (item.direction === 'ltr') {
return 'ltr';
} else {
return 'rtl';
}
} …
Run Code Online (Sandbox Code Playgroud)采取以下打字稿:
type Primitive = undefined | null | boolean | number | string;
// A POJO is just supposed to be your typical object, with nothing
// fancy, but where we don't exactly know what's in it.
interface POJO {
[key: string]: Primitive | POJO;
}
Run Code Online (Sandbox Code Playgroud)
好的,所以 POJO 应该代表一些通用对象,但是是一个很好的简单对象。可能是我要去的事情JSON.stringify
或者其他什么。现在这是有问题的代码:
// Some functions implemented elsewhere...
declare function post(path: string, data: POJO): Promise<Response>;
declare function getToken(): string;
type LinkOrMessage = {link: string} | {message: string};
function specialPost(path: string, data: LinkOrMessage): …
Run Code Online (Sandbox Code Playgroud) 我目前有以下代码:
const pattern = "quick";
const re = new RegExp(pattern, "gi");
const string = "The quick brown fox jumped over the lazy QUICK dog";
const replaced = string.replace(pattern, "<b>" + pattern + "</b>");
console.log(replaced);
Run Code Online (Sandbox Code Playgroud)
它产生以下内容:
The <b>quick</b> brown fox jumped over the lazy QUICK dog
Run Code Online (Sandbox Code Playgroud)
我想要的是:
The <b>quick</b> brown fox jumped over the lazy <b>QUICK</b> dog
Run Code Online (Sandbox Code Playgroud)
我有两个问题。
首先,为什么QUICK
我不区分大小写的正则表达式不被替换?
其次,我如何确保QUICK
用<b>QUICK</b>
和代替<b>quick</b>
?
我试图更好地理解 TypeScript。我发现如果我写这样的错误:
function doStuff() {
if (physician.email == "george@gmail.com")
{
var physician = {email: "bob@gmail.com", name: "Dr. Bob"};
/* .... */
} }
Run Code Online (Sandbox Code Playgroud)
TypeScript 不会发现在我们尝试使用其属性之一之后定义了医师对象的问题。这会导致运行时错误:
类型错误:无法读取未定义的属性“电子邮件”。
为什么 TypeScript 转译器不捕捉这样的东西?我相信使用 TypeScript 2.0。
I have an array of objects, each with non correlative ids.
I would like to know if it is possbile, given an integer, to find the next closest id value object.
Example
const array = [{id: 4}, {id: 10}, {id: 15}]
Run Code Online (Sandbox Code Playgroud)
given any value x
, if 10 < x <= 15
, it should return 15
given any value x
, if 4 < x <= 10
, it should return 10
and so on.
Thank you
我正在尝试创建一个程序,它将取一个数字中的数字并相互相乘.那583
就是5*8*3 = 120
.
它没有按预期工作,只是返回了number
它.
我该如何解决?
这是代码:
function persistence(num) {
//code me
numString = num.toString();
numArray = numString.split().map(function(t) {
return parseInt(t)
});
function reducer(theNumArray) {
let sum = 1;
for (var i = 0; i < theNumArray.length; i++) {
sum = sum * theNumArray[i];
}
return sum;
}
newNum = reducer(numArray);
console.log(newNum);
};
persistence(485);
Run Code Online (Sandbox Code Playgroud)
我有一个关于将数组转换为对象的问题.查看数组中的两个数组:
[
[ 'answer 1 question 1',
'answer 2 question 1',
'answer 3 question 1',
'answer 4 question 1'
],
[ 'answer 1 question 2',
'answer 2 question 2',
'answer 3 question 2',
'answer 4 question 2'
]
]
Run Code Online (Sandbox Code Playgroud)
请解释我如何将此数组转换为以下格式:
[
[
{'name': 'answer 1 question 1'},
{'name': 'answer 2 question 1'},
{'name': 'answer 3 question 1'},
{'name': 'answer 4 question 1'}
],
[
{'name': 'answer 1 question 2'},
{'name': 'answer 2 question 2'},
{'name': 'answer 3 question 2'},
{'name': …
Run Code Online (Sandbox Code Playgroud) javascript ×8
typescript ×3
arrays ×2
regex ×2
any ×1
async-await ×1
for-in-loop ×1
for-loop ×1
function ×1
generator ×1
inference ×1
intersection ×1
lodash ×1
node.js ×1
object ×1
promise ×1