在TS类中,函数类型的引用属性和实例方法有什么不同?看到有些视频中两者都混着写

来源:2-13 【TS类晋级】深入TypeScript引用属性和它的4个真实应用场景-1

mohf3126361

2022-05-08

写回答

1回答

keviny79

2022-05-08

1.  const fn=()=>{ ...}   const obj={...}  fn,obj是引用属性,引用属性 也可以说成 引用变量。 

2.  实例变量调用的方法就是实例方法。TS  class Customer(){  step(){  }}   let cust=new Customer()  cust.step() 中的step就是一个实例方法,

0
1
mohf3126361
// 问题1: 请问一下, Demo类实例方法的两种写法(写法1 和 写法2)有什么不同吗? 实际场景中用的是哪一种? // 问题2: 另外在接口中也有类似的这两种写法, 也帮忙解释一下? class Demo { x_point: number; y_point: number; constructor(x_ponit_: number, y_ponit_: number) { this.x_point = x_ponit_; this.y_point = y_ponit_; } // 实例方法——写法1(类似匿名函数表达式写法) getDistance1 = (x: number, y: number): number => { return Math.pow(x - this.x_point, 2) + Math.pow(y - this.y_point, 2); }; // 实例方法——写法2(类似命名函数写法) getDistance2(x: number, y: number): number { return Math.pow(x - this.x_point, 2) + Math.pow(y - this.y_point, 2); } } const demo = new Demo(1, 2); console.log(demo.getDistance1(3, 4)); console.log(demo.getDistance2(3, 4)); // 请问一下, Demo类实例方法的两种写法(写法1 和 写法2)有什么不同吗? 实际场景中用的是哪一种? // 另外在接口中也有类似的这两种写法, 也帮忙解释一下? interface AA { x: number; y: number; getDistance: (x: number, y: number) => number; } interface BB { x: number; y: number; getDistance(x: number, y: number): number; }
2022-05-09
共1条回复

晋级TypeScript高手,成为抢手的前端开发人才

轻松驾驭 TypeScript 高级用法, 突破前端成长瓶颈

871 学习 · 425 问题

查看课程