前端基础(二十四):label语句、with语句、JSON、生成器、解析赋值、历史状态管理、将页面可编辑

本文深入探讨了JavaScript中的高级特性,包括使用label语句控制循环流程、利用with语句设置作用域、通过JSON.stringify进行对象序列化时的自定义转换、使用生成器实现迭代器模式,以及变量的解析赋值等技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

label语句

let data = { a: 'a', b: 'b', c: 'c', d: 'd', e: 'e' }

// break 跳出switch循环,但是for循环继续
// no a => b => no c => no d => no e
for (key in data) {
    switch (key) {
        case "b":
            console.log(key);
            break;
        default:
            console.log('no', key);
    }
}

// label语句 当满足条件后跳出for循环
// no a => b
outloop: for (key in data) {
    switch (key) {
        case "b":
            console.log(key);
            break outloop;
        default:
            console.log('no', key);
    }
}

width 将作用域设置到特定的对象中

a = 'AAA';
b = 'BBB';
let data = { a: 'aaa' }
with(data){
    console.log(a); // aaa
    console.log(b); // BBB
}

JSON stringify

var foo = { name: "Lee", age: 24, sex: '男' };
var jsonString = JSON.stringify(foo, (key, value) => {
    console.log(key, value); // name Lee --- age 24 --- sex 男
    if (typeof value === 'number') {
        return undefined
    }
    return value
});
console.log(jsonString); // {"name":"Lee","sex":"男"}

const students = [
    {
        name: 'akira',
        score: 100,
    }, 
    {
        name: 'John',
        score: 60,
    }, 
    {
        name: 'Jane',
        score: 90,
    }
];
let jsonStudents = JSON.stringify(students, (key, value) => {
    if (key === 'score') {
        if (value === 100) {
            return 'S';
        } else if (value >= 90) {
            return 'A';
        } else if (value >= 70) {
            return 'B';
        } else if (value >= 50) {
            return 'C';
        } else {
            return 'E';
        }
    }
    return value;
});
console.log(jsonStudents); // [{"name": "akira","score": "S"},{"name": "John","score": "C"},{"name": "Jane","score": "A"}]

let jsonStudentsNames = JSON.stringify(students, ['name']);
console.log(jsonStudentsNames); // [{"name":"akira"},{"name":"John"},{"name":"Jane"}]

JSON parse

let json = '[{"name": "akira","score": "S"},{"name": "John","score": "B"},{"name": "Jane","score": "A"}]';
let arr = JSON.parse(json, (key, value)=>{
    if(key === 'score'){
        switch(value){
            case 'S':
                return '优';
            case 'A':
                return '甲';
            case 'B':
                return '乙';
        }
    }
    return value;
})
console.log(arr); // [{name: "akira", score: "优"},{name: "John", score: "乙"},{name: "Jane", score: "甲"}]

生成器

function* addCount(){
    let count = 0;
    while (true) {
        yield count++
    }
}
let add = addCount();

console.log(add.next()); // {value: 0, done: false}
console.log(add.next()); // {value: 1, done: false}
console.log(add.next()); // {value: 2, done: false}

解析赋值

var [name, age] = ['Lee', 24];
console.log(name);
console.log(age);

交换

let a = 1;
let b = 2;
[a, b] = [b, a]
console.log(a); // 2
console.log(b); // 1

对象解析赋值

let obj = {
    name: 'Lee',
}

let {name} = obj; // => let {name: name} = obj; 
console.log(name); // Lee

let {name: a} = obj;
console.log(a); // Lee
let obj = {
    name: 'Lee',
    age: 24
}
let {name: _name, age: _age} = obj;
console.log(_name); // Lee
console.log(_age);  // 24

pushState popstate

window.onload = function () {
    pushHistory();
    setTimeout(function () { 
        window.addEventListener('popstate', function (event) {
            let e = event || window.event;
            console.log(e);
        }, false);
    }, 100);
}
function pushHistory() {
    window.history.pushState({ name: "Lee" }, "----", "home.html");
}

页面可编辑

document.designMode = 'on'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Prosper Lee

您的赏赐将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值