JS/TS笔记学习1

周末总得学点什么吧~
奥利给!

  • 跑火车 递归 减速
javascript 复制代码
let currentIndex = 0;  
let speed = 500; // 初始速度,单位是毫秒  
let decrement = 20; // 每次迭代速度减少的量  
  
const cells = document.querySelectorAll('.cell');  
  
function highlightCell() {  
  cells.forEach((cell, index) => {  
    cell.classList.remove('highlight');  
    if (index === currentIndex) {  
      cell.classList.add('highlight');  
    }  
  });  
  
  if (speed > 50) { // 当速度降低到一定程度时停止  
    speed -= decrement;  
    setTimeout(highlightCell, speed);  
    currentIndex = (currentIndex + 1) % cells.length; // 移动到下一个格子  
  }  
}  
  
highlightCell(); // 开始跑火车
  • 队列
javascript 复制代码
/**
     * 将弹窗加入弹出窗队列
     * @param {string} panelPath 
     * @param {string} scriptName 
     * @param {*} param 
     */
    public pushToPopupSeq(panelPath: string, scriptName: string, param: any) {
        let popupDialog = {
            panelPath: panelPath,
            scriptName: scriptName,
            param: param,
            isShow: false
        };

        this._arrPopupDialog.push(popupDialog);

        this._checkPopupSeq();
    }
    /**
     * 检查当前是否需要弹窗
     */
    private _checkPopupSeq() {
        if (this._arrPopupDialog.length > 0) {
            let first = this._arrPopupDialog[0];

            if (!first.isShow) {
                this.showDialog(first.panelPath, first.param);
                this._arrPopupDialog[0].isShow = true;
            }
        }
    }
    /**
     * 将弹窗从弹出窗队列中移除
     * @param {string} panelPath 
     */
    public shiftFromPopupSeq(panelPath: string) {
        this.hideDialog(panelPath, () => {
            if (this._arrPopupDialog[0] && this._arrPopupDialog[0].panelPath === panelPath) {
                this._arrPopupDialog.shift();
                this._checkPopupSeq();
            }
        })
    }
  • ts给readonly属性赋值
    除了在声明它的类或构造函数内部被赋值,有些情况下(静态只读)可能需要赋值
javascript 复制代码
class TweenSystem {  
    static readonly instance; // 声明静态只读属性  如何赋值呢??? 请看下面  
    // ... 其他代码保持不变 ...  
    public getNum(){
        console.log(123);
    }
}
//这种情况如何赋值只读属性呢,看下面
javascript 复制代码
// 在类外部初始化 instance  
(TweenSystem.instance as any) = new TweenSystem();  
// 现在可以正确访问了  
const actionMgr = TweenSystem.instance;
actionMgr.getNum();   //123

那么为什么as any后就可以赋值(TweenSystem.instance as any);

TweenSystem.instance as any 的情况下,通过类型断言将 TweenSystem.instance 的类型临时转换为 any,从而能够给它赋值。尽管这样做在技术上是可行的,但它破坏了 TypeScript 的类型安全原则,并可能导致运行时错误或难以追踪的 bug。
通常,不建议使用类型断言来修改 readonly 属性,除非有非常明确的原因,并且完全理解可能带来的后果。如果需要在类的外部初始化 readonly 属性,更好的做法是在类的内部进行初始化

  • 单例模式
    懒汉式 ,是延迟加载 的方式,只有使用的时候才会加载
javascript 复制代码
class Singleton {  
    // 使用一个私有的静态实例变量来存储类的唯一实例  
    private static instance: Singleton | null = null;  
    // 构造函数是私有的,这样外部就无法使用 new Singleton() 来创建新的实例  
    private constructor() {  
        // 初始化代码(如果有的话)  
    }  
    // 提供一个静态方法来获取类的唯一实例  
    // 如果实例不存在,则创建它  
    public static get getInstance(): Singleton {  
        if (Singleton.instance === null) {  
            Singleton.instance = new Singleton();  
        }  
        return Singleton.instance;  
    }  
    // 类的其他方法或属性  
    public someMethod() {  
        console.log("这是 Singleton 类的一个方法");  
    }  
}  
// 使用示例  
const singletonInstance = Singleton.getInstance;  
singletonInstance.someMethod(); // 调用 Singleton 类的方法   //out: 这是 Singleton 类的一个方法

而有些情况下也可以直接这样
饿汉式立即加载的方式

javascript 复制代码
class Singleton1 {  
    static readonly instance = new Singleton1();  
    // 构造函数是私有的,这样外部就无法使用 new Singleton1() 来创建新的实例  
    private constructor() {  
        // 初始化代码(如果有的话)  
        console.log("在编译完就直接创建了,不管你有没有调用")
    }
    // 类的其他方法或属性  
    someMethod() {  
        console.log("这是 Singleton1 类的一个方法");  
    }  
}  
  
// 使用示例  
const Singleton1Instance = Singleton1.instance;  
Singleton1Instance.someMethod(); // 调用 Singleton1 类的方法


饿汉式和懒汉式的使用场景,看业务需求,如果业务上允许有比较充分的启动和初始化时间,就使用饿汉式,否则就使用懒汉式;

相关推荐
Jackyzhe4 小时前
Flink学习笔记:整体架构
笔记·flink
徒 花4 小时前
初级网安作业笔记1
笔记
kfepiza4 小时前
Debian-10编译安装Mysql-5.7.44 笔记250706
linux·数据库·笔记·mysql·debian·bash
西西西仓鼠4 小时前
python学习打卡:DAY 40 训练和测试的规范写法
学习
Magnetic_h5 小时前
【iOS】方法与消息底层分析
笔记·学习·macos·ios·objective-c·cocoa
今天背单词了吗9805 小时前
算法学习笔记:19.牛顿迭代法——从原理到实战,涵盖 LeetCode 与考研 408 例题
笔记·学习·算法·牛顿迭代法
晓13136 小时前
JavaScript加强篇——第四章 日期对象与DOM节点(基础)
开发语言·前端·javascript
DKPT6 小时前
Java设计模式之行为型模式(观察者模式)介绍与说明
java·笔记·学习·观察者模式·设计模式
烛阴6 小时前
JavaScript函数参数完全指南:从基础到高级技巧,一网打尽!
前端·javascript
future14127 小时前
C#进阶学习日记
数据结构·学习