Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

设计模式1——起步 #46

Open
L-small opened this issue Jul 19, 2020 · 0 comments
Open

设计模式1——起步 #46

L-small opened this issue Jul 19, 2020 · 0 comments

Comments

@L-small
Copy link
Owner

L-small commented Jul 19, 2020

很多设计模式的看起来是几乎一模一样的,其在具体场景解决了什么问题才是我们辨别模式的关键。

动态语言

JS是动态语言类型。

静态语言:编译时就能发现代码中的错误,比如类型错误等。缺点是增加了代码量。
动态语言:编写代码量少,更灵活。缺点是很多错误运行期才能发现。

鸭子类型

鸭子类型其实就是基于接口编程,我们不关注具体实现,只要满足接口的方式就可以。

多态

同一操作作用在不同的对象上,产生不同的解释和不同的执行结果。而多态的思想就是将不变的事物和可能变化的事物进行分离。而在JS中体现的更多的是通过把过程化的条件分支语句转化为对象的多态性,从而消除这些条件分支语句,并将行为分布在各个对象中,让这些对象各自负责自己的行为。

const googleMap = {
  show: () => {
    console.log('googleMap')
  }
}
const baiduMap = {
  show: () => {
    console.log('baiduMap')
  }
}
const renderMap = function(map) {
  if (map.show instanceof function) {
    map.show()
  }
}
renderMap(googleMap)
renderMap(baiduMap)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant