javascript中的原型模型可以学到什么?

发布于 2024-07-15 04:06:55 字数 1187 浏览 8 评论 0原文

这个问题是从语言设计的角度来看的。

我应该解释一下情况。 我正在开发一个不支持原型的 javascript 变体,但是它早该有一个像样的类型系统(最重要的是对 instanceof 的支持)。 ecmascript 规范并不重要,所以我可以自由地实现一些不同的、更适合的东西。

在变体中: -

  • 您不使用 function foo() 声明构造函数,而是在模板文件中声明构造函数,这意味着构造函数存在于命名空间中(由文件的路径决定)
  • 目前所有继承行为是通过应用模板来完成的,这意味着所有共享函数都被复制到每个单独的对象(毕竟没有原型)。

我从来都不是一名 Web 开发人员,这让我处于一个有点奇怪的境地:我从来没有在愤怒中使用过原型。 尽管这并没有阻止我对他们有意见。

据我了解,我对原型模型的主要问题是

  • 地乱扔对象名称空间、obj.prototype、obj.constructor(这是一个不成熟的反对意见,试图保留将对象视为映射的能力,但它们可能不是?)
  • 不必要 在运行时更改共享行为似乎没有必要,直接使用额外的间接级别会更直接obj.shared.foo()。 特别是,这是一个相当大的实现难题,
  • 人们通常似乎不太理解原型,例如原型和构造函数之间的区别。

因此,为了解决这些问题,我的想法是使用一个特殊的运算符 constructorsof。 基本上,原则是每个对象都有一个构造函数列表,您有时会想要访问这些构造函数。

var x = new com.acme.X();
com.acme.Y(x,[]);      // apply y

(constructorsof x) // [com.acme.Y,com.acme.X,Object];

x instanceof com.acme.X;   // true
x instanceof com.acme.Y;   // true

感谢所有反馈,我知道可能很难理解我的观点,因为我想传达很多内容,但这是一个重要的决定,专家的意见可能是无价的。

  • 任何可以提高我对原型模型的理解的东西,无论是好的还是坏的。
  • 对我的提案的想法

谢谢,

迈克

编辑:提案希望现在有意义。

The question is from a language design perspective.

I should explain a little about the situation. I am working on a javascript variant which does not support prototypes, however it is overdue a decent type system (most importantly support for instanceof). The ecmascript spec is not important, so i have the freedom to implement something different and better suited.

In the variant:-

  • You do not declare constructors with function foo(), rather constructors are declared in template files, which means constructors exist in a namespace (detirmined by the path of the file)
  • Currently all inheritance of behaviour is done by applying templates which means all shared functions are copied to each individual object (there are no prototypes afterall).

Never having been a web developer, this puts me in the slightly bizarre position of never having used prototypes in anger. Though this hasn't stopped me having opinions on them.

My principal issues with the prototype model as i understand it are

  • unnecessary littering of object namespace, obj.prototype, obj.constructor (is this an immature objection, trying to retain ability to treat objects as maps, which perhaps they are not?)
  • ability to change shared behaviour at runtime seems unnecessary, when directly using an extra level of indirection would be more straight forward obj.shared.foo(). Particularly it is quite a big implementation headache
  • people do not seem to understand prototypes very well generally e.g. the distinction between a prototype and a constructor.

So to get around these my idea is to have a special operator constructorsof. Basically the principal is that each object has a list of constructors, which occasionally you will want access to.

var x = new com.acme.X();
com.acme.Y(x,[]);      // apply y

(constructorsof x) // [com.acme.Y,com.acme.X,Object];

x instanceof com.acme.X;   // true
x instanceof com.acme.Y;   // true

All feedback appreciated, I appreciate it maybe difficult to appreciate my POV as there is a lot i am trying to convey, but its an important decision and an expert opinion could be invaluable.

  • anything that can improve my understanding of the prototype model, the good and the bad.
  • thoughts on my proposal

thanks,

mike

edit: proposal hopefully makes sense now.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

浮萍、无处依 2024-07-22 04:06:55

Steve Yegge 写了一篇关于原型模型。

Steve Yegge has written a good technical article about the prototype model.

寻梦旅人 2024-07-22 04:06:55

我认为您对原型模型的问题是无效的:

  • 对象命名空间、obj.prototype、obj.constructor 中不必要的垃圾

prototype 是构造函数的属性,而不是对象实例的属性。 此外,由于 [[DontEnum]] 属性,问题并不像听起来那么糟糕,不幸的是无法以编程方式设置。 如果可以的话,一些明显的问题就会消失。

这是一个不成熟的反对意见,试图保留将对象视为地图的能力,但它们可能不是?

只要键是字符串并且检查 hasOwnProperty(),使用对象作为映射就没有问题。

  • 在运行时更改共享行为的能力似乎没有必要,因为直接使用额外的间接级别会更直接 obj.shared.foo()。 特别是它是一个相当大的实施难题

我不明白实现原型链的最大实现难题在哪里。 事实上,我认为原型继承在概念上比基于类的继承更简单,而基于类的继承在具有后期绑定的语言中没有提供任何好处。

  • 人们似乎不太了解原型,例如原型和构造函数之间的区别。

只了解 Java 和 C++ 等基于类的 oo 语言的人不了解 JavaScript 的继承系统,新闻 11。

除了 MarkusQ 的建议之外,您可能还想查看 Io

I don't think your issues with the prototype model are valid:

  • unnecessary littering of object namespace, obj.prototype, obj.constructor

prototype is a property of the contructor function, not the object instance. Also, the problem isn't as bad as it sounds because of the [[DontEnum]] attribute, which unfortunately can't be set programatically. Some of the perceived problems would go away if you could.

is this an immature objection, trying to retain ability to treat objects as maps, which perhaps they are not?

There's no problem with using objects as maps as long as the keys are strings and you check hasOwnProperty().

  • ability to change shared behaviour at runtime seems unnecessary, when directly using an extra level of indirection would be more straight forward obj.shared.foo(). Particularly it is quite a big implementation headache

I don't see where the big implementation headache in implementning the prototype chain lies. In fact, I consider prototypical inheritance conceptually simpler than class-based inheritance, which doesn't offer any benefits in languages with late-binding.

  • people do not seem to understand prototypes very well generally e.g. the distinction between a prototype and a constructor.

People who only know class-based oo languages like Java and C++ don't understand JavaScript's inheritance system, news at 11.

In addition to MarkusQ's suggestions, you might also want to check out Io.

微凉徒眸意 2024-07-22 04:06:55

使用实际代码尝试一些事情可能会更容易。 使用一种简单的语法(无论是什么语法)创建语言,并用该语言实现某些内容。 然后,经过几次重构迭代后,找出阻碍阅读和编写代码的功能。 添加、更改或删除改进语言所需的内容。 这样做几次。

确保你的测试代码真正运用了你的语言的所有部分,即使有一些确实试图破坏它的部分。 尝试在测试中做所有错误的事情(以及所有正确的事情)

It might just be easier to try a few things with practical code. Create the language with one simple syntax, whatever that is, and implement something in that language. Then, after a few iterations of refactoring, identify the features that are obstacles to reading and writing the code. Add, alter or remove what you need to improve the language. Do this a few times.

Be sure your test-code really exercises all parts of your language, even with some bits that really try to break it. Try to do everything wrong in your tests (as well as everything right)

能否归途做我良人 2024-07-22 04:06:55

阅读“self”这种开创原型模型的语言,可能会帮助您不仅仅是从 JavaScript 角度思考它(特别是因为您似乎像许多人一样将其与“Web 编程”联系起来)。 一些帮助您入门的链接:

http://selflanguage.org/
http://www.self-support.com/

请记住,那些未能学习历史的人注定要重新学习历史。

Reading up on "self", the language that pioneered the prototype model, will probably help you more than just thinking of it in terms of javascript (especially since you seem to associate that, as many do, with "web programming"). A few links to get you started:

http://selflanguage.org/
http://www.self-support.com/

Remember, those who fail to learn history are doomed to reimplement it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文