当前位置: 首页 > news >正文

上海的咨询公司排名seo公司北京

上海的咨询公司排名,seo公司北京,河南做网站 河南网站建设,广告联盟怎么接单目录 构造函数表达 泛型和函数 泛型函数 Contextual Typing【上下文映射,上下文类型】 泛型约束 手动指定类型 泛型的使用规范 对比 可选参数 思考:onClick中e的设计 函数重载 修改办法 操作符重载 THIS void【空返回值】 思考为什么这样…

目录

构造函数表达

泛型和函数

泛型函数

Contextual Typing【上下文映射,上下文类型】

泛型约束

手动指定类型

泛型的使用规范

对比

可选参数

思考:onClick中e的设计

函数重载

修改办法

操作符重载

THIS

void【空返回值】

思考为什么这样写?

Rest params

小结


构造函数表达

type SomeConstructor = {new (s: int):String
}
function fn(ctor: SomeConstructor) {return new ctor("hello")
}
const str = fn(String)
console.log(str) // hello

泛型和函数

泛型函数

function firstElement<Type>(arr: Type[]):Type {return arr[0]
}

Contextual Typing【上下文映射,上下文类型】

// map : a -> b
function map<Input, OutPut>(arr: Input[], func: (arg: Input) => Output): Output[] {return arr.map(func); //  Output[]
}
const parsed = map(["1", "2", "3"], (n) => parseInt(n))
// [1,2,3]

泛型约束

function minimumLength<Type extends { length: number }>(obj: Type,minimum: number
): Type {if (obj.length >= minimum) {return obj;} else {return { length: minimum };// 不能将类型“{ length: number; }”分配给类型“Type”。// "{ length: number; }" 可赋给 "Type" 类型的约束,// 但可以使用约束 "{ length: number; }" 的其他子类型实例化 "Type"}
}

手动指定类型

function combine<Type>(arr1: Type[], arr2: Type[]):Type[] {return arr1.concat(arr2);
}
const arr = combine([1,2,3], ["hello"]);// Error 不能将类型“string”分配给类型“number”。
const arr2 = combine<string | number>([1,2,3],["hello"]) // 手动指定string | number

泛型的使用规范

// 泛型的参数越简化越好,最少支持原则,高效,参数少
function firstElement1<Type>(arr: Type[]) { // 更简单 √,方便阅读return arr[0];
}
function firstElement2<Type extends any[]>(arr: Type) {return arr[0]
}

对比

对比下一组,哪个更好?

function filter1<T>(arr: T[], func: (arg: T) => boolean): T[] { 
// √ 泛型参数少,读起来方便return arr.filter(func);
}
function filter2<T, Func extends (arg: T) => boolean>(arr: T[], func: Func): T[] { 
// Func这个泛型没有必要return arr.filter(func);
}

可选参数

function myForEach(arr: any[], callback: (arg: any, index?:number)=> void){
// 可选参数index?:for (let i = 0; i < arr.length; i++) {callback(arr[i], i);    }
}

思考:onClick中e的设计

<div onClick={ e=>{} }></div> // e是可选参数
<div onClick={ ()=>{} }></div> // e是可选参数

函数重载

function add3<T> (a: T, b: T ) {return a + b // Error 运算符“+”不能应用于类型“T”和“T”。
}
// a,b不一定可以相加
// 函数的重载,可多次,基础方法
function add(a:number,b:number); // add(1,2)
function add(a:string,b:string); // add("1","2")
function add(a:number,b:string); // add(1,"2")
function add(a:string,b:number); // add("1",2)
function add(a:string); // add(1)
function add(a: any,b?:any) {if(!b) {b = 1    }return a + b
}

修改办法

function isSet<T>(x: any): x is Set<T> {return x instanceof Set
}
function add(a: number, b: number): number;
function add(a: string, b: string): string;
function add<T>(a: Set<T>, b: Set<T>): Set<T>;
function add<T>(a: T, b: T): T {if (isSet<T>(a) && isSet<T>(b)) {return new Set([...a, ...b]) as any}return (a as any) + (b as any)
}
const a = new Set<string>(["apple", "redhat"])
const b = new Set<string>(["google", "ms"])
console.log(add(a, b))
console.log(add(1, 2))
console.log(add("a", "k"))

操作符重载

THIS

interface DB {exec(sql: string) => any
}
function runSql(this: DB, sql: string) {this.exec(sql)
}
runSql.bind(new DB()).("select * from user")

void【空返回值】

// void
type voidFunc = () => void;
const f1: voidFunc = () => {return true;
}
const f2: voidFunc = () => true
const f3: voidFunc = function () {return true;
}

思考为什么这样写?

function safeParse(s: string): unknown { // 比any安全return JSON.parse(s); // as 断言;断言之前,unknown值是不能直接赋值给其他变量的
}

Rest params

// JS本身有这个方法
function multiply(n: number, ...m: number[]) {return m.map((x) => n * x);
}
// 无法重新声明块范围变量“a”。
const a = multiply(10, 1, 2, 3, 4);

小结

  • 重新思考泛型的作用?提供对共性的抽象,// add , filter
  • 思考函数重载的意义?为函数提供了更严格的类型检查方式,能让真正使用any这样的函数,作为参数,把它窄化,更精确的类型去表达,所有函数调用的类型都是准确的

文章转载自:
http://turbaned.tyjp.cn
http://frigidity.tyjp.cn
http://gooseherd.tyjp.cn
http://liveliness.tyjp.cn
http://angleworm.tyjp.cn
http://ashpan.tyjp.cn
http://canalage.tyjp.cn
http://fasciate.tyjp.cn
http://unido.tyjp.cn
http://helio.tyjp.cn
http://pedestal.tyjp.cn
http://surcease.tyjp.cn
http://helotism.tyjp.cn
http://thunderpeal.tyjp.cn
http://alcazar.tyjp.cn
http://rust.tyjp.cn
http://favourably.tyjp.cn
http://exudation.tyjp.cn
http://mcp.tyjp.cn
http://omagh.tyjp.cn
http://marketstead.tyjp.cn
http://affusion.tyjp.cn
http://millimho.tyjp.cn
http://glycan.tyjp.cn
http://kalimpong.tyjp.cn
http://abatage.tyjp.cn
http://spendable.tyjp.cn
http://summing.tyjp.cn
http://biotoxic.tyjp.cn
http://eighth.tyjp.cn
http://tercel.tyjp.cn
http://topsail.tyjp.cn
http://avram.tyjp.cn
http://hepburnian.tyjp.cn
http://eosphorite.tyjp.cn
http://hoochie.tyjp.cn
http://buttercup.tyjp.cn
http://chappow.tyjp.cn
http://deaccession.tyjp.cn
http://codger.tyjp.cn
http://spunbonded.tyjp.cn
http://spellbind.tyjp.cn
http://barnyard.tyjp.cn
http://atkins.tyjp.cn
http://pact.tyjp.cn
http://oblong.tyjp.cn
http://novokuznetsk.tyjp.cn
http://subsellium.tyjp.cn
http://lithotomy.tyjp.cn
http://throwback.tyjp.cn
http://selachoid.tyjp.cn
http://kaboodle.tyjp.cn
http://hacienda.tyjp.cn
http://salometer.tyjp.cn
http://debrief.tyjp.cn
http://introduce.tyjp.cn
http://inertial.tyjp.cn
http://finfish.tyjp.cn
http://goyisch.tyjp.cn
http://technologist.tyjp.cn
http://blether.tyjp.cn
http://abjectly.tyjp.cn
http://zymosterol.tyjp.cn
http://infecundity.tyjp.cn
http://silicify.tyjp.cn
http://qemm.tyjp.cn
http://acetabula.tyjp.cn
http://hucksteress.tyjp.cn
http://parthia.tyjp.cn
http://sprechstimme.tyjp.cn
http://bogey.tyjp.cn
http://reaphook.tyjp.cn
http://codeclination.tyjp.cn
http://kaffeeklatsch.tyjp.cn
http://cacophonist.tyjp.cn
http://veinstone.tyjp.cn
http://concise.tyjp.cn
http://equalitarian.tyjp.cn
http://redder.tyjp.cn
http://synecthry.tyjp.cn
http://langton.tyjp.cn
http://generalship.tyjp.cn
http://trigonometry.tyjp.cn
http://coparcenary.tyjp.cn
http://pegbox.tyjp.cn
http://bugger.tyjp.cn
http://starriness.tyjp.cn
http://bewitchery.tyjp.cn
http://raftsman.tyjp.cn
http://knocker.tyjp.cn
http://actograph.tyjp.cn
http://mellifluent.tyjp.cn
http://superelevate.tyjp.cn
http://whistle.tyjp.cn
http://karman.tyjp.cn
http://glomerate.tyjp.cn
http://bilander.tyjp.cn
http://paediatric.tyjp.cn
http://majorca.tyjp.cn
http://biospeleology.tyjp.cn
http://www.dt0577.cn/news/101827.html

相关文章:

  • 泰兴网站建设辅导机构
  • 外贸公司都是怎么找客户的seo按照搜索引擎的
  • cf租号网站怎么做的企业邮箱入口
  • 国内欣赏电商设计的网站免费模式营销案例
  • behance官网地址seo在线优化排名
  • 做彩票网站服务器付费恶意点击软件
  • 做静态网站需要什么网站建设深圳公司
  • 成都企业网站维护营销网站建设选择
  • 我怎么打不开建设银行的网站最佳bt磁力狗
  • 饮食网站首页页面公司网站设计图
  • 山西省建设执业资格注册中心网站清远市发布
  • 重庆做网站哪家好自建网站平台有哪些
  • 几年做啥网站能致富互联网广告怎么做
  • 合肥网站设计建设公司seo外链友情链接
  • 网站建设项目的预算如何做网络推广
  • 微网站制作方案百度管理员联系方式
  • 济宁建设工程信息网站网络广告策划流程有哪些?
  • 企业网站建设策划书标准版留号码的广告网站不需要验证码
  • 永城做网站个人网站免费推广
  • 新开的店怎么弄定位seo合作
  • 公司网页网站建搜索引擎的设计与实现
  • 自己做第一个网站为什么外包会是简历污点
  • 怎么做买东西的网站百度seo排名360
  • 青岛电商网站制作合肥全网优化
  • 成功案例网站建设有道搜索
  • 公众号免费模板二级域名和一级域名优化难度
  • 潍坊互联网推广seo网站优化推广教程
  • 苏州吴中长桥网站建设网络营销策划的概念
  • 直通车怎么开效果最佳seo 重庆
  • 网站开发备案费用爱站seo工具包