2

I come from a Java background, where everything is Object Oriented. While Getting into a bit more of Javascript (more into the class areas of javascript), I've noticed complete changes. The biggest for me is getting used to the prototyping of the so-called "classes" javascript has. So, my question is if you need to intialize the varialbes you pass into your class function constructor method-thing. For example:

function Foo(a, b, c) {
    this.a = a;
    var b = b;
    this.c = "";

    this.d = a + b;
}

Now In javascript is this necessary? Cause in Java, you have to show that the variable type definition in the argument itself: Foo(int a, int b, string c) Now how does the method in Javascript know what type of data structure it is being passed too? Like what if they passed in an array for a, and then my code tried to add the integer and the array together? That won't push the int too the array will it?

Sorry for being a bit questiony, I've been looking for an answer for a while on the Google... And it's getting late here.

Thanks for any help

Uneveris

user253751
  • 57,427
  • 7
  • 48
  • 90
UNEVERIS
  • 249
  • 1
  • 2
  • 8
  • 3
    Javascript is dynamically typed. – Elliott Frisch Feb 01 '15 at 08:17
  • This question is not related to Java - I've removed that tag. – user253751 Feb 01 '15 at 08:31
  • ... whoops, yes it is. My bad. (I'll leave the edit, since it's still not a Java question per se, but anyone else who disagrees can undo it) – user253751 Feb 01 '15 at 08:32
  • Because js isn't strongly typed you'll get unexpected results when trying to do operations with unexpected data types. Closure compiler, typescript and dart can help detecting such mistakes and you can do some programmatic type checking. Not sure what your question is but if you're asking if its a good idea to use constructor functions and or factory functions in JavaScript the answer would be yes because that would be the answer for any object oriented code no matter the language used. More on prototype can be ound here: http://stackoverflow.com/a/16063711/1641941 – HMR Feb 01 '15 at 09:04

2 Answers2

2

So, my question is if you need to intialize the varialbes you pass into your class function constructor method-thing.

Do they need initializing, no. Javascript is a loosely typed language and declared variables can be of any type.

You do not need to declare a variable type for the arguments, they can be anything. Also note the vars are private variables in the scope of the constructor.

Now how does the method in Javascript know what type of data structure it is being passed too?

As a result of loose types, javascript has a type typeof to help work out what a variables type actually is if strong typing is required.

if (typeof this.a !== 'function')
    throw "Expected a function, received a " + typeof this.a;

Verbose, but it fulfils its purpose.

typeof reference

Like what if they passed in an array for a, and then my code tried to add the integer and the array together? That won't push the int too the array will it?

Have you tried to do this?

var a = new Array();
var b = 1;
var c = a + b;
console.log(typeof c);

>> string

In Node.js the output is a string with the array values concatenated and the integer appended as a string on the end.

It is important when expecting a specific data structure that data you have been passed is what you are expecting. In JS, this is by conditionally checking.

If you are writing these classes purely for your self, duck typing can be useful. If it looks like a duck, quacks like a duck then it is a duck. This is to do with semantics when working in a loosely typed language like JS. Two assumptions === true.

What is duck typing

Hope this helps answer your questions.

Community
  • 1
  • 1
David Barker
  • 14,484
  • 3
  • 48
  • 77
0

You can't be sure what parameter types are being passed to your method. That is a main Javascript language trait which can be used for both good and bad.

So, what happens if there are wrong parameter types? Javascript will try to silently convert them to something common. For example:

var a = [100,2,3];
var b = 5;
var c = a + b;

Adding an array and an integer (as well as string) will result in a string "100,2,35". Note that array is first converted to String, then 5 is simply appended to the end. This behaviour closely resembles the Java's one, that calls a toString() method of any object whenever it needs to concatenate.

You can avoid the wrong types in two ways. First, convert them yourself. Like,

a = Number.parseInt(a, 10); // now a is of type number

Second, if your method is important and highly dependent on the data correctness, you should not convert the params but avoid using them at all if they are of wrong type:

if (typeof a != "number") throw "Param must be int";

Finally, you can see this JS framework that supports strict typing:

http://betterjs.org/

Andrei Nikolaenko
  • 1,022
  • 1
  • 7
  • 13