Some notes on Javascripts

JS array can store list of different types of data. Thus they act like the lists in Python. Also, their position is fixed, thus they are ordered, just like lists in Python but unlike dictionaries in Python. They have 0-based indexing.

Use <arr>.push() method to push elements to the array.

String(<other type var>) for type-casting.

isNaN(<var>) behaves in a strange way with the quoted numbers (e.g. “23”). It automatically type-casts them to a number and then returns false. However, if it is to behave like equivalent functions in other languages, it should return true for isNaN(“23”), since “23” is a string.

JS allows creation of jagged array. i.e. two-dimensional arrays with variable number of columns i.e. variable number of elements in each row.

for/in loop behaves like a combination of for loop from JS (in syntax) and comprehensions from Python (in behavior).

[Doubtfull!]No semicolon at the end of the constructor is required. Constructors are a special kind of methods.

We can have functions performing different functions in the constructor.

<classname>.prototype.<new Method> = function() {statements;}; is the format to change the prototype of a class and add a method named <new Method> to the class, thus making it accessible to all of the objects.

<childClass>.prototype = new <parent-class-constructor>; is the inheritance in JS.

In JS all properties of an object are public by default. To create a private property(variable) or method, define the variable, method inside the constructor using var keyword instead of using this.<property/method_name>. Write get and set methods inside the constructor to access these variables/methods.


IS IT POSSIBLE TO ADD THE GET AND SET METHODS LATER BY PROTOTYPING??????????????????????????????????????????



Functions can return functions as well (like Pyhton). This is how you access the private methods of a class: by accessing them through a public method.

hasOwnProperty() comes from Object.prototype.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: