2

I am new to Scala and still exploring this language with huge number of constructs and features. Going by the way Class constructor's syntax is, it looks a lot similar to Constructor function in JavaScript.

Can anyone point out the similarities and the differences there are between the two.

1
  • 1
    "Constructor" functions in JavaScript are ad-hoc, while Scala's Constructor functions are an intentional language feature. Commented Jan 25, 2012 at 20:33

1 Answer 1

5

I think it is not correct to compare Javascript and Scala in this case. Javascript is prototype based and dynamically typed, scala is statically typed and has classic inheritance model (+ multiple inheritance via traits).

So, similarity - constructor in both languages is used to initialize new instance of a class, the other - syntax, implementation, nuances - all the different. Scala has alternative contructors, JS - not, scala's constructor's parameters given on a class itself, in JS - it is a function which is called with new operator, etc.

Maybe you meant Java, not JavaScript?

Sign up to request clarification or add additional context in comments.

2 Comments

I am not comparing the two languages at all. JS is a lot diff than any other traditional object oriented language. or the new ones. What I was looking for was differences like : alternative constructors as u hv rightly mentioned. My way of lookin at the similarity was from the the fact that there was one construct accepting params in both the cases and using them in all its member functions. JS doesnt hv class and Scala's constructor is blended into its class definition so they were looking the same to me.
Actually that blended constructor is just syntactic sugar for conciseness. It is treated as separate method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.