JavaScript原型继承的缺点是什么? [英] The disadvantages of JavaScript prototype inheritance, what are they?

查看:127
本文介绍了JavaScript原型继承的缺点是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近观看了 Douglas Crockford的JavaScript演示他热衷于JavaScript原型继承,好像它是切片白面包以来最好的东西。考虑到Crockford的声誉,它可能很好。

I recently watched Douglas Crockford's JavaScript presentations, where he raves about JavaScript prototype inheritance as if it is the best thing since sliced white bread. Considering Crockford's reputation, it may very well be.

有人可以告诉我JavaScript原型继承的缺点是什么? (例如,与C#或Java中的类继承相比)

Can someone please tell me what is the downside of JavaScript prototype inheritance? (compared to class inheritance in C# or Java, for example)

推荐答案

在Javascript中对现有对象进行子类化时我想念的事情与C ++中的类继承:

Things I miss when sub-classing an existing object in Javascript vs. inheriting from a class in C++:


  1. 没有标准的(内置于语言)编写方式看起来相同无论哪个开发人员编写它。

  2. 编写代码并不会像C ++中类头文件那样自然地生成接口定义。

  3. 没有标准的方法来执行受保护的和私有的成员变量或方法。对于某些事情有一些惯例,但不同的开发人员也会采用不同的方式。

  4. 当你在定义中犯了愚蠢的输入错误时,没有编译步骤告诉你。

  5. 当你需要它时没有类型安全。

  1. No standard (built-into-the-language) way of writing it that looks the same no matter which developer wrote it.
  2. Writing your code doesn't naturally produce an interface definition the way the class header file does in C++.
  3. There's no standard way to do protected and private member variables or methods. There are some conventions for some things, but again different developers do it differently.
  4. There's no compiler step to tell you when you've made foolish typing mistakes in your definition.
  5. There's no type-safety when you want it.

不要误会我的意思,有一个javascript原型继承的方式与C ++相比具有很多优势,但这些是我发现javascript工作不太顺畅的地方。

Don't get me wrong, there are a zillion advantages to the way javascript prototype inheritance works vs C++, but these are some of the places where I find javascript works less smoothly.

4和5与严格无关原型继承,但是当你有一个包含许多模块,许多类和大量文件的大型项目并且你希望重构某些类时,它们会发挥作用。在C ++中,您可以更改类,更改尽可能多的调用者,然后让编译器找到需要修复的所有剩余引用。如果您已添加参数,更改的类型,更改的方法名称,移动的方法等...编译器将显示您需要解决的问题。

4 and 5 are not strictly related to prototype inheritance, but they come into play when you have a significant sized project with many modules, many classes and lots of files and you wish to refactor some classes. In C++, you can change the classes, change as many callers as you can find and then let the compiler find all the remaining references for you that need fixing. If you've added parameters, changed types, changed method names, moved methods,etc... the compiler will show you were you need to fix things.

在Javascript中,没有简单的方法来发现需要更改的所有可能的代码片段,而无需逐字执行每个可能的代码路径,看看你是否遗漏了某些东西或者做了一些错字。虽然这是javascript的一般缺点,但我发现它在重构大型项目中的现有类时特别有用。我已经接近一个大型JS项目的发布周期结束,并决定我不应该进行任何重构来解决问题(即使这是更好的解决方案),因为没有找到所有可能的后果的风险JS中的变化远远高于C ++。

In Javascript, there is no easy way to discover all possible pieces of code that need to be changed without literally executing every possible code path to see if you've missed something or made some typo. While this is a general disadvantage of javascript, I've found it particularly comes into play when refactoring existing classes in a significant-sized project. I've come near the end of a release cycle in a significant-sized JS project and decided that I should NOT do any refactoring to fix a problem (even though that was the better solution) because the risk of not finding all possible ramifications of that change was much higher in JS than C++.

因此,我发现在JS项目中进行某些类型的与OO相关的更改风险更大。

So, consequently, I find it's riskier to make some types of OO-related changes in a JS project.

这篇关于JavaScript原型继承的缺点是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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