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)
这段代码很混乱。有一个更好的方法吗?
考虑以下三种类型中,MainType是工会Type1和Type2。如果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) 下面的片段解释了这个问题
//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.5Run Code Online (Sandbox Code Playgroud)
为什么null和undefined在数组内部和外部转换为字符串时显示不同的行为。
从
"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)
我能做什么 ??请回答我。谢谢你。
请参见下面的代码段。
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 Object并num.hasOwnProperty('prop')返回,false但仍然可以访问Object.prototypeon 的属性num
我需要你的帮助!我有一个已知坐标的点,例如{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 实现以前创建的函数。该函数接收一个对象。具有以下属性
tag: 这将是一个string.children: 是同一个接口的数组(即ceProps如下图);style:这将是含有样式等(对象color,fontSize等)innerHTML,src等)这是通过代码。
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) 我使用 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)
现在我做了一个减速器功能。对于所有类型的动作,我使用了两个道具name和data. name将始终是 astring并且数据将是一种类型,具体取决于name
type ActionTypes = {name: "removeCountry", data: string} | …Run Code Online (Sandbox Code Playgroud) 我正在调试某人的代码,想知道此正则表达式的作用是什么?
码:
<script>
var t = document.getElementById("filterVal").value;
var s = filterVal.replace(/"([^"]+(?="))"/g, '$1')
</script>
Run Code Online (Sandbox Code Playgroud) 我有一个类型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 ×10
typescript ×4
arrays ×2
jquery ×2
css ×1
ecmascript-6 ×1
object ×1
react-hooks ×1
variables ×1