0

Is it possible to cast a custom object into another custom object in Javascript? For example, in the Java implementation of the code I am trying to convert into Javascript, they have done the following:

return ((TimeSeriesPoint)tsArray.get(pointIndex)).get(valueIndex);

where tsArray is an ArrayList and TimeSeriesPoint is a custom class for which the implementation exists. Is it possible to perform such casting into TimeSeriesPoint in Javascript?

Thanks in advance

akash
  • 22,664
  • 11
  • 59
  • 87
elbereth
  • 37
  • 1
  • 4
  • 10
  • In javascript there is no type safety for objects as I know. you can assign one object to another without any casting. – vikas Oct 06 '14 at 17:11
  • 1
    Casting should not be necessary. See [duck typing](http://stackoverflow.com/questions/4205130/what-is-duck-typing). – superEb Oct 06 '14 at 17:12

2 Answers2

1

There are no types for objects in JavaScript, so there is no casting.

For what you want to do, your TimeSeriesPoint constructor should take one point as an argument and construct a new object from it, so that you can invoke it like

return (new TimeSeriesPoint(tsArray.get(pointIndex)).get(valueIndex);

Alternatively, if your points in the array are very similar to timeseries-points (and qualify as them via duck typing, i.e. have the same instance properties), you could just apply the TimeSeriesPoint get method on them:

return TimeSeriesPoint.prototype.get.call(tsArray.get(pointIndex), valueIndex);
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0

In javascript there is no type safety for objects as I know. you can assign one object to another without any casting.

Please see below example.

var x= []; // x is an array
x = {}; // Now x changes to object
vikas
  • 931
  • 6
  • 11