有没有“正确”的在JavaScript中进行继承的方法?如果是这样,它是什么? [英] Is there a "right" way to do inheritance in JavaScript? If so, what is it?

查看:122
本文介绍了有没有“正确”的在JavaScript中进行继承的方法?如果是这样,它是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力学习如何在现有代码中添加测试 - 目前正在阅读有效工作使用旧版代码。我一直试图在JavaScript中应用一些原则,现在我正在尝试提取一个接口。

I have been trying to learn how to add testing to existing code -- currently reading reading Working Effectively With Legacy Code. I have been trying to apply some of the principles in JavaScript, and now I'm trying to extract an interface.

在JavaScript中搜索创建接口时,我可以'找到了很多 - 我发现的继承似乎有几种不同的方式。 (有些人创建自己的基类来提供有用的方法,使其更容易进行继承,有些使用函数,有些使用原型)。

In searching for creating interfaces in JavaScript, I can't find a lot -- and what I find about inheritance seems like their are several different ways. (Some people create their own base classes to provide helpful methods to make it easier to do inheritance, some use functions, some use prototypes).

什么是正确的方法?有一个简单的例子用于在JavaScript中提取界面吗?

What's the right way? Got a simple example for extracting an interface in JavaScript?

推荐答案

没有明确的正确方法,因为有这么多人做了很多不同的事情。有很多有用的模式。

There's no definitive right way, because so many people are doing so many different things.. There are many useful patterns.

Crockford 表明你顺其自然,或者以与javascript的原型性质相对应的方式编写javascript。

Crockford suggests that you "go with the grain", or write javascript in a way that corresponds to javascript's prototypal nature.

当然,他继续表明Netscape建议的原始模型实际上已被破坏。他将其标记为伪经典,并指出了遵循该模型所涉及的许多误导和不必要的复杂性。

Of course, he goes on to show that the original model that Netscape suggested is actually broken. He labels it "pseudoclassical", and points out a lot of the misdirection and unnecessary complexity that is involved in following that model.

他将对象函数写为补救措施(现在称为Object.create())。它允许一些非常强大的原型模式。

He wrote the "object" function as a remedy (now known as Object.create() ). It allows for some very powerful prototypal patterns.

当你必须使用传统的javascript时,开发一个干净的界面并不总是那么容易,特别是当你处理时具有大型系统,通常包括多个库,并且每个系统实现独特的样式和不同的继承模式。一般来说,我要说继承的正确方法是允许你编写一个干净的接口,它在遗留代码的上下文中表现良好,但也允许你重构并消除旧的依赖关系。 。

It's not always easy to do develop a clean interface when you have to work with legacy javascript, especially not when you're dealing with large systems, usually including multiple libraries, and each implementing a unique style and different inheritance pattern. In general, I'd say that the "right way" to do inheritance is the one which allows you to write a clean interface which behaves well in the context of your legacy code, but also allows you to refactor and eliminate old dependencies over time.

考虑到主要库模式之间的差异,我发现在我自己的工作中最成功的路线是保持我的接口独立于库接口完全。如果它有用,我将使用库或模块,但不会受其约束。这让我可以重构很多代码,逐步淘汰一些库,并使用库作为脚手架,以后可以优化。

Considering the differences between the major library patterns, I've found that the most successful route to take in my own work is to keep my interfaces independent of the library interfaces entirely. I'll use a library or module if it's helpful, but won't be bound to it. This has allowed me to refactor a lot of code, phase out some libraries, and use libraries as scaffolding which can be optimized later.

沿着这些方向,我写了受Crockford寄生遗传模式启发的界面。这真的是一个简单的胜利。

Along these lines, I've written interfaces that were inspired by Crockford's parasitic inheritance pattern. It's really a win for simplicity.

另一方面,我确信你可以争取选择一个图书馆,在你的团队中执行它,并遵守它的继承模式和接口约定。

On the other side of the coin, I'm sure you could argue for picking a library, enforcing it across your team, and conforming to both its inheritance patterns and its interface conventions.

这篇关于有没有“正确”的在JavaScript中进行继承的方法?如果是这样,它是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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