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

2
  • In javascript there is no type safety for objects as I know. you can assign one object to another without any casting. Commented Oct 6, 2014 at 17:11
  • 1
    Casting should not be necessary. See duck typing. Commented Oct 6, 2014 at 17:12

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.