简体   繁体   English

有什么类似于Java中JavaScript的“原型”吗?

[英]Is there anything similar to JavaScript's “prototype” in Java?

Right, so I know Java fairly well but I never studied extensively into JavaScript. 是的,所以我对Java相当了解,但是我从未深入研究JavaScript。 I was chatting with an acquaintance who knows JavaScript, but who has not studied into Java. 我正在和一个认识JavaScript的熟人聊天,但是他还没有研究Java。 He was telling me about the advantages of JavaScript over Java in terms of how it functions but not its uses. 他向我介绍了JavaScript比Java的优势,即它的功能而不是用途。

Specifically, he was telling me about "prototyping" and what he explained seems to be the concepts of polymorphism and inheritance, but classless. 具体来说,他是在告诉我有关“原型”的知识,他所解释的似乎是多态性和继承的概念,但没有类。 Is this true? 这是真的?

I told this person that it seems this way, and he told me that prototyping is nothing at all like inheritance or polymorphism, and he told me to research it. 我告诉这个人,这似乎是这样,他告诉我原型制作根本不像继承或多态性,他告诉我进行研究。 Google isn't helping me out much in terms of comparing the two languages so I'm here in hopes of finding someone who knows both and can shed some light here. Google在比较这两种语言方面并没有提供太多帮助,因此我在这里希望找到能够同时了解这两种语言的人。

It is similar in a sense of prototype is a parent for all objects aka functions in javascript that inherit from it. 从某种意义上说,它是相似的,它是所有对象的父代,也就是javascript中继承自它的函数。 Java inheritance is much more well defined in extending objects and using interfaces and abstract classes. Java继承在扩展对象以及使用接口和抽象类方面定义得更好。 Javascript has low type abstraction. Javascript具有低类型抽象。 Everything is a function. 一切都是功能。 Everything can have properties tacked on at will. 一切都可以随意添加属性。 There is no defined inheritance. 没有定义的继承。 If you follow a stack trace of a call in JavaScript it starts at the top most function and goes down to the prototype until it finds the call your looking for. 如果您跟踪JavaScript中调用的堆栈跟踪,则它将从最顶部的函数开始,一直到原型,直到找到您要查找的调用为止。 Java on the other hand will look at the direct function or variable call and return. 另一方面,Java将查看直接函数或变量的调用和返回。 It will also traverse but upwards towards the Object parent. 它也将遍历对象父对象,但向上移动。 Everything in JavaScript is based off of function while in Java all objects are based off of Object. JavaScript中的所有内容均基于功能,而Java中的所有对象均基于Object。

Polymorphism is achieved by calls on Object.create in JavaScript vs just extending in Java. 通过调用JavaScript中的Object.create而不是Java中的扩展来实现多态。 OOJS is way different in prototypical inheritance vs object inheritance. OOJS在原型继承与对象继承方面截然不同。 In Java 在Java中

Javascript is a functional language so objects in Javascript are created by functions. Javascript是一种功能语言,因此Javascript中的对象是由函数创建的。 Java is Object oriented to functions (methods) belong to objects. Java是面向对象的,函数(方法)属于对象。

Java script is also dynamically typed so objects get a type at runtime and do have some element of inheritance and polymorphism but it is not enforced through a static typing system like java's classes. Java脚本也是动态类型的,因此对象在运行时获得类型,并且确实具有继承和多态性的元素,但是它不是通过诸如Java类的静态类型系统来强制执行的。

Prototyping is associated with the process of writing a function to create a new object of a particular type and declaring that an existing type is the prototype of the new type. 原型与编写函数以创建特定类型的新对象并声明现有类型是该新类型的原型的过程相关。

The new type will have all the properties of the old type plus any newly declared ones. 新类型将具有旧类型的所有属性以及任何新声明的属性。 So it is sort of like inheritance and polymorphism but the is no typing to enforce it so all you will get at runtime if you step outside the definition of a class is a undefined value. 因此,它有点像继承和多态,但是没有类型来强制执行它,因此,如果您超出类的定义范围,则在运行时将得到的所有值都是未定义的值。

From a use perspective the client code of any object can regard it as any prototype up the chain so yes inheritance and polymorphism. 从使用角度来看,任何对象的客户端代码都可以将其视为链上的任何原型,因此可以继承和实现多态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM