TS面向对象笔记

笔记

数组是引用类型,=[]等于复制引用地址,数组改变,赋值变量也一起改变
深拷贝:开辟一个新的地址去赋值

let a=[1,2,3]
let b=[...a]
a[0]=100
转换成json,再转换成结构体,实现深拷贝
let b=JSON.parse(JSON.Stringify(a));


let obj={
	age:18,
	fn(){console.log(this.age}
}
用展开运算符赋值,函数修改是重写,所以对函数没影响
let obj2={...obj}
万能递归实现深拷贝
function deepClone(oldData){
	if(typeof oldData==='object'&&oldData!==null){
		let res=Array.isArray(oldData)?[]:{};开辟新类型并赋值
		for(let K in oldData){
			if(oldData.hasOwnProperty()K){判断是否还有子属性
			res[K]=deepClone(oldData[K]);
			}
		}
		return res;
	}else{
		return oldData;
	}

}

防抖,只触发最后一次的修改
延迟修改,等待1秒后触发
let oInput=document.querySelector('input')
let t=null
input = function(){
	if(t !== null){
		clearTimeout(t);
	}
	t=setTimeout(()=>{
		console.log(this.value)
	},1000);
}
修改闭包形式
let oInput=document.querySelector('input')
oInput.oninput=debounce(function(){
	console.log(this.value);
},1000);
function debounce(fn,delay){
	let t=null;
	return function(){
		if(t!==null){
			clearTimeout(t);
		}
		t=setTimeout(()=>{
			fn.call(this);
		},delay);
	}
}

节流:只有定时器结束才变为true才触发,否则不触发

```typescript
let oInput=document.querySelector('input')
window.onscroll=throttle(function(){
	alert("广告");
},2000);
function throttle(fn,delay){
	let t=true;
	return function(){
		if(t){
			setTimeout(()=>{
				fn.call(this);
				t=true;
			},delay);
		}
		t=false;
	}
}

Promise, async, await

异步
https://rexdainiel.gitbooks.io/typescript/content/docs/promise.html
setTimeout(()=>{},3000)

fetch()
.then(()=>{})追加操作
捕获异常
.catch((error)=>console.error(error))
链条结束后调用
.finally()

async function f(){
	const perA=await fetch()并行
	const perB=await fetch()
}
f()

const [a,b]=await Promise.all([perA,perB])非并行


[1,2,3]forEach(
async(i)=>{
	await console.log()
	虽然有await但是foreach会立即返回,不会等待所有执行操作完成
	for循环为等待执行
	for wait()for循环并发执行,执行完成后往下执行
}
)

封装Promise函数,正常执行返回resolve,用.then((res)调用
        return new Promise((resolve, reject)=>{
            
            resources.load("tt",(err, res: JsonAsset)=>{
                if (err) {
                    console.log(err);
                    return;
                }
                console.log("-----")
                this.jsonData = res.json!;
                resolve( res.json!)   
        })
})



cocos 两种同步方法

init2() {
        return new Promise((resolve, reject)=>{
            
            resources.load("tt",(err, res: JsonAsset)=>{
                if (err) {
                    console.log(err);
                    return;
                }
                console.log("-----")
                this.jsonData = res.json!;
                resolve( res.json!)   
        })
})
}

this.init2()      
        .then((res)=>{
            this.jsonData=<object>res;
            console.log('--'+this.jsonData.data[0].gameURL)})async bbxx(){
    const ad = new AsyncDelegate();
    ad.add(() => {
        return new Promise((resolve, reject) => {
            resources.load("tt",(err, res: JsonAsset)=>{
               
               this.bb=res.json!
               resolve();
           });
        })
        
    });
    //console.log(this.bb.data[0].gameURL)
    await ad.dispatch();
    console.log(this.bb.data[0].gameURL)
 }

this.bbxx()

直接引入

直接引入
import express form 'express';
const app=express();

app.get('/',function(req,res){
	res.send('hell')
})
app.listen(3000)
console.log('auth 服务')

单例
private static instance:Input=null
static get Instance(){
if(this.instance==null){
this.instance=new Input()
}
return this.instance
}

Inupt.Instance

自身
目标
内容

class message{
Type:number
Command:number
Content:any
constructor(type,command,content)
}
class messageType{
static Type_IU=1
static Type_NPC=2
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值