最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Is Javascript constructor function equivalentsimilar to a class or interface in Java - Stack Overflow

programmeradmin0浏览0评论

I am trying to pick up the basics of Java and I am more familiar with JavaScript.

Is the following statement accurate (I just need high level understanding):

Javascript constructor function or factory function is the equivalent (i am using this word loosely here) of a class or interface in Java.

EDIT:

This is what I am reading in a Java book:

A Java program is mostly a collection objects talking to other objects by invoking each other's methods. Every object is of a certain type, and that type is defined by a class or an interface. Most Java programs use a collection of many different types.

Coming from Javascript, above sounds very much like JS constructor function is similar to a class in Java where the objects properties and methods would be defined.

I know Java and JavaScript are two separate languages.

Thanks

I am trying to pick up the basics of Java and I am more familiar with JavaScript.

Is the following statement accurate (I just need high level understanding):

Javascript constructor function or factory function is the equivalent (i am using this word loosely here) of a class or interface in Java.

EDIT:

This is what I am reading in a Java book:

A Java program is mostly a collection objects talking to other objects by invoking each other's methods. Every object is of a certain type, and that type is defined by a class or an interface. Most Java programs use a collection of many different types.

Coming from Javascript, above sounds very much like JS constructor function is similar to a class in Java where the objects properties and methods would be defined.

I know Java and JavaScript are two separate languages.

Thanks

Share Improve this question edited Jan 9, 2014 at 17:25 Andy asked Jan 9, 2014 at 17:07 AndyAndy 5512 gold badges4 silver badges15 bronze badges 8
  • Java classes have constructors, if that is what you are asking. Java interfaces do not. – crush Commented Jan 9, 2014 at 17:09
  • 1 @crush OP is speaking of constructors in JS, not saying Java classes and interfaces have constructors. – user395760 Commented Jan 9, 2014 at 17:10
  • 1 It sounds to me like he's asking if a JS constructor is equivalent to a class or an interface in Java. – crush Commented Jan 9, 2014 at 17:10
  • @crush Yes, that's my understanding as well. But in that case your ment makes no sense. – user395760 Commented Jan 9, 2014 at 17:11
  • 1 @crush The thing is, "constructor" means rather different things in JS and Java, even if the two may be parable (of which I am not sure). You appear to be confusing two concepts which share the same term. – user395760 Commented Jan 9, 2014 at 17:14
 |  Show 3 more ments

5 Answers 5

Reset to default 6

I'd say you're close. Constructor functions (and prototypes) in JavaScript are the closest thing to Java classes that we have in JS; but they're certainly not "equivalent".

You can dynamically add or remove properties and methods to the prototype of a JavaScript constructor; you can't add or remove things from a Java class at runtime.

Example:

function Foo() {}
Foo.prototype.hello = function() { alert('hello'); };

var f = new Foo();
f.hello(); // alerts 'hello'

delete Foo.prototype.hello;

f.hello(); // throws an error

You can achieve "inheritance" at runtime in JavaScript simply by assigning the prototypes of constructor functions to arbitrary objects. In Java you declare inheritance at pile-time and it cannot be changed at runtime.

Example:

function EnglishSpeaker() {}
EnglishSpeaker.prototype.greet = function() { return 'hello'; };

function SpanishSpeaker() {}
SpanishSpeaker.prototype.greet = function() { return 'hola'; };

function Me() {}
Me.prototype = EnglishSpeaker.prototype;

var me = new Me();
me instanceof EnglishSpeaker; // true
me.greet(); // 'hello'

Me.prototype = SpanishSpeaker.prototype;
me = new Me();
me instanceof EnglishSpeaker; // false
me instanceof SpanishSpeaker; // true
me.greet(); // 'hola'

In JavaScript a prototype is simply an object. So a "class" (constructor function) can "inherit" from any plain object; thus there is a much looser distinction between "types" and "values".

Example:

function Thing() {}

var randomObject = { foo: 1, bar: 2 };
Thing.prototype = randomObject;

var thing = new Thing();
thing.foo; // 1

In Java you can define an interface which some class must implement. JavaScript doesn't really provide any such mechanism.

These are just some of the differences off the top of my head. Point is: they're similar, and you're right to draw a connection. But they are definitely not the same.

JavaScript:

function Cat(name) {
    this.name = name;
    this.talk = function() {
        alert( this.name + " says meeow!" );
    };
} 

var cat1 = new Cat("Felix");

in Java:

public class Cat {
    private String name;

    public Cat(String name) {
        this.name = name;
        this.talk();
    }

    public void talk() {
        System.out.println( this.name + " says meeow!" );
    }
} 

Cat cat1 = new Cat("Felix");

From source :Does JavaScript have the interface type (such as Java's 'interface')?

JavaScript inheritance is based on objects, not classes. That's not a big deal until you realize: JavaScript is an extremely dynamically typed language -- you can create an object with the proper methods, which would make it conform to the interface, and then undefine all the stuff that made it conform. It'd be so easy to subvert the type system -- even accidentally! that it wouldn't be worth it to try and make a type system in the first place.

If you're ing from JavaScript and learning Java, there's one huge difference that I don't think anyone else has mentioned yet.

In JavaScript, a function is an object. An object can contain properties whose values can be function objects, or they can be other objects or primitive values. When you call obj.method(arguments), the semantics are to look for a method property of the object (or a prototype), and if it's a function object, to call it.

In Java and other piled languages with OOP features, functions are not objects. If you create a new object of a particular type, the "type" information in the object refers to a list of polymorphic functions for that type. That's how polymorphism works in these languages. If you call obj.method(arguments), and method is a method that can be overridden for derived types, then the program looks up obj's type, and then looks in the function list for the type to determine which function to call. What this means, to me, is that the object's type is a key piece of information in these languages, and OOP revolves around it; while in JavaScript, the "type" of an object is quite a bit less important, since the function itself is looked up by name in the object. One can use JavaScript in a way to make it look like it's emulating the way other languages handle OOP, but it's not required, and it's not a built-in part of the language.

I think you have to keep that in mind when going from one to the other. If you try too hard to relate Java features to JavaScript features you're already familiar with, it will be confusing.

If you are new to Java, I suggest you go to amazon. and look for a beginning book on java that has good reviews and read it cover to cover. I think this will be the best use of your time rather than reading varoius articles on Java and picking it up piece-meal. I also suggest you don't try to port your knowledge of Javascript to Java, least you make learning Java that much harder. Note there are many books you can read for Java (web) development: Java, products built on Java (JSF, Tomcat, etc), and supporting technologies (HTML, CSS, XML, etc).

发布评论

评论列表(0)

  1. 暂无评论