1、typescript
interface Shape {draw: () => void;}class Rectangle implements Shape {draw() {console.log('draw Rectangle')}}class Square implements Shape {draw() {console.log('draw Square')}}class Circle implements Shape {draw() {console.log('draw Circle')}}class ShapeMaker {private circle: Shape;private rectangle: Shape;private square: Shape;constructor() {this.circle = new Circle();this.rectangle = new Rectangle();this.square = new Square();}drawCircle() {this.circle.draw();}drawRectangle() {this.rectangle.draw();}drawSquare() {this.square.draw();}}const maker = new ShapeMaker()maker.drawCircle()maker.drawRectangle()maker.drawSquare()
2、javascript (apply)
这里借一下aplly方法来模拟一下
var _type = 'string'Object.prototype.toString.apply(_type) // "[object String]"var _type = 123Object.prototype.toString.apply(_type) // "[object Number]"var _type = nullObject.prototype.toString.apply(_type) // "[object Null]"
