鸭子在 Javascript 中打字 [英] Duck Typing in Javascript

查看:28
本文介绍了鸭子在 Javascript 中打字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能给我一个 JavaScript 中 Duck Typing 继承的例子?我正在探索 OO javascript,我听说过鸭子类型,但看不到它在 javascript 中使用的任何示例.

Could someone give me an example of Duck Typing inheritance in Javascript? I'm exploring OO javascript and I've heard about duck typing but can't see any examples of it being used in javascript.

推荐答案

第二个链接给出了 js 中类似鸭子类型的模式的示例.不是说我建议这样做,但是......好吧,你要求这样做.;)

The second link gives an example of a duck-typing-like pattern in js. Not saying I recommend doing this, but... well, you asked for it. ;)

在计算机编程中面向对象的编程语言,鸭子打字是一种动态风格输入对象的当前一组方法和属性确定有效的语义,而不是比它从特定的继承特定的类或实现界面.

In computer programming with object-oriented programming languages, duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface.

维基百科 - 鸭子打字

最简单的方法是定义非正式地签订合同,简单地依靠两边的开发者界面以了解他们在做什么.Dave Thomas 给出了这种方法鸭子打字"的名字——如果它会走路像一只鸭子,它像鸭子一样嘎嘎叫,那么它是一只鸭子.同样与我们的Shape 接口,如果它可以计算一个面积和周长,那么它是形状.

The simplest approach is to define the contract informally and simply rely on the developers at each side of the interface to know what they are doing. Dave Thomas has given this approach the name of "duck typing" —if it walks like a duck and it quacks like a duck, then it is a duck. Similarly with our Shape interface, if it can compute an area and a perimeter, then it is a Shape.

JavaScript 接口和鸭子打字

这篇关于鸭子在 Javascript 中打字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆