小编Mah*_*Ali的帖子

用JavaScript在两种颜色之间切换的最佳方法?

Javascript初学者在这里。我本质上是想进行简单的切换。如果元素为黑色,则将其更改为白色。如果是白色,请将其更改为黑色。

function changeClass() {
    if (document.getElementById('myButton').style.backgroundColor == "white") {
        document.getElementById('myButton').style.backgroundColor = "black";
    } else {
        document.getElementById('myButton').style.backgroundColor = "white";
    }
}
Run Code Online (Sandbox Code Playgroud)
<button class="normal" id="myButton" onclick='changeClass()' >Change Colour</button>
Run Code Online (Sandbox Code Playgroud)

这段代码很混乱。有一个更好的方法吗?

javascript css

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

如何让 Discriminated Unions 与解构一起工作?

考虑以下三种类型中,MainType是工会Type1Type2。如果kind"kind1"那么data应该是{msg: string}相同的类型Type2

interface Type1 {
    kind: "kind1";
    data: { msg: string };
}
interface Type2 {
    kind: "kind2";
    data: { msg2: string };
}

type MainType = Type1 | Type2;
Run Code Online (Sandbox Code Playgroud)

这是使用它的第一种方法。

function func(obj: MainType) {
    switch (obj.kind) {
        case "kind1": return obj.data.msg;
        case "kind2": return obj.data.msg2;
    }
}
Run Code Online (Sandbox Code Playgroud)

上面的代码没有给出错误并显示正确的自动完成。

但是当我们解构obj它时,它会出错。

function func({kind, data}: MainType) {
    switch (kind) {
        case "kind1": return data.msg;
        case …
Run Code Online (Sandbox Code Playgroud)

javascript typescript

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

将数组转换为字符串时,数组中的“null”和“undefined”在哪里?

下面的片段解释了这个问题

//Outside array
console.log(String(undefined)); //undefined
console.log(String(null));      //null
console.log(String(5.55));      //5.55

//inside array

console.log(String([undefined])); // ""
console.log(String([null])); // ""
console.log(String([5.55])); //5.5
Run Code Online (Sandbox Code Playgroud)

为什么nullundefined在数组内部和外部转换为字符串时显示不同的行为。

javascript arrays variables

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

javascript从对象数组中获取键名

"data":[{"ja":"???"},{"en":"Osaka"}]
Run Code Online (Sandbox Code Playgroud)

我想得到"ja""en"

我尝试了几种方法...

data.map(function(_, i) { return i; });
Run Code Online (Sandbox Code Playgroud)

它返回数字数组。

console.log(Object.keys(Object.values(data)));
Run Code Online (Sandbox Code Playgroud)

所有的审判都回来了

(2) [0, 1]
0: 0
1: 1
Run Code Online (Sandbox Code Playgroud)

我能做什么 ??请回答我。谢谢你。

javascript jquery

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

为什么数字从Object.prototype继承时不是Object的实例?

请参见下面的代码段。

let num = 3;
Object.prototype.prop = "something";
console.log(num instanceof Object); //false;
console.log(num.hasOwnProperty('prop')) //false

console.log(num.prop) //'something'
Run Code Online (Sandbox Code Playgroud)

有人可以解释原因num instanceof Objectnum.hasOwnProperty('prop')返回,false但仍然可以访问Object.prototypeon 的属性num

javascript object ecmascript-6

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

按距离给定的升序对点数组进行排序

我需要你的帮助!我有一个已知坐标的点,例如{x:5, y:4}和代表每个点的对象数组:

[{x:2,y:6},{x:14,y:10},{x:7,y:10},{x:11,y:6},{x:6,y:2}]
Run Code Online (Sandbox Code Playgroud)

现在,我需要按与给定点的距离以升序对数组进行排序,例如:

[{x: 6, y: 2}, {x: 2, y: 6}, {x: 7, y: 10}, {x: 11, y: 6}, {x: 14, y: 10}]
Run Code Online (Sandbox Code Playgroud)

我如何用JS做到这一点???谢谢!

javascript arrays

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

如何为在打字稿中创建嵌套元素结构的函数创建接口?

我是打字稿的新手。我正在使用 javascript 实现以前创建的函数。该函数接收一个对象。具有以下属性

  • tag: 这将是一个string.
  • children: 是同一个接口的数组(即ceProps如下图);
  • style:这将是含有样式等(对象colorfontSize等)
  • 可以将任何其他键添加到此对象。(例如innerHTMLsrc等)

这是通过代码。

interface Style { 
    [key : string ]: string;
}

interface ceProps {
    tag: string;
    style?: Style;
    children?: ceProps[];
    [key : string]: string;
}

const ce = ({tag, children, style, ...rest } : ceProps) => {
    const element = document.createElement(tag);

    //Adding properties
    for(let prop in rest){
        element[prop] = rest[prop];
    }

    //Adding children
    if(children){
        for(let child of …
Run Code Online (Sandbox Code Playgroud)

javascript typescript

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

如何在打字稿中创建字符串的子类型

我使用 typescript 和 react hooks 制作了一个名为 risk 的游戏。游戏是在某种地图上进行的。所以首先我有设计 I MapEditor。地图编辑器的状态就像

export interface IMapEditorState {
   mousePos: IPoint;
   countries: {[k: string]: ICountry};
   continents: { [k: string]: IContinent };
}
Run Code Online (Sandbox Code Playgroud)

countries并且continents是对象。国家的界面看起来像

//The "name" property and above key will be same in `{[k: string]: ICountry};` will be same
export interface ICountry {
   name: string;
   border: IDot[];
   neighbours: string[];
   completed: boolean;
}
Run Code Online (Sandbox Code Playgroud)

现在我做了一个减速器功能。对于所有类型的动作,我使用了两个道具namedata. name将始终是 astring并且数据将是一种类型,具体取决于name

type ActionTypes = {name: "removeCountry", data: string} | …
Run Code Online (Sandbox Code Playgroud)

javascript typescript react-hooks

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

这个正则表达式做什么(/“([^”] +(?=“))” / g,'$ 1')?

我正在调试某人的代码,想知道此正则表达式的作用是什么?

码:

<script>

 var t = document.getElementById("filterVal").value;
 var s = filterVal.replace(/"([^"]+(?="))"/g, '$1')

</script>
Run Code Online (Sandbox Code Playgroud)

javascript jquery

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

如何将元组联合转换为对象类型

我有一个类型InputData,要么是要么string是元组。我想将其转换为对象。

type InputType = File | string | boolean;
type InputData = string | [string, InputType]

type test = "username" | "password" | ["isAbove18", boolean] | ["photo", File];
Run Code Online (Sandbox Code Playgroud)

在上面的类型中,只有string(给出了输入字段的名称)它的类型应该设置为string. 我有通用类型

type AddDefaultValues<Data extends InputData> = Data extends string ? [Data, string] : Data 
//AddDefaultValues<test> = ["username", string]| ["password", string] | ["isAbove18", boolean] | ["photo", File]
Run Code Online (Sandbox Code Playgroud)

现在我的要求是将上面的类型映射到如下所示的对象。

{username: string, password: string, isAbove18: boolean, photo: File}
Run Code Online (Sandbox Code Playgroud)

我尝试了很长时间,但什么也做不了。请帮我解决这个问题...

javascript typescript

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