Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare array of specific type in javascript

Is it possible in java script to explicitly declare array to be an array of int(or any other type)?

something like var arr: Array(int) would be nice...

like image 774
31415926 Avatar asked Apr 14 '14 13:04

31415926


2 Answers

var StronglyTypedArray=function(){
      this.values=[];
      this.push=function(value){
      if(value===0||parseInt(value)>0) this.values.push(value);
      else return;//throw exception
     };
     this.get=function(index){
        return this.values[index]
     }
   }

EDITS: use this as follows

     var numbers=new StronglyTypedArray();
     numbers.push(0);
     numbers.push(2);
     numbers.push(4);
     numbers.push(6);
     numbers.push(8);
     alert(numbers.get(3)); //alerts 6
like image 189
Bellash Avatar answered Oct 21 '22 06:10

Bellash


Array of specific type in typescript

export class RegisterFormComponent 
{
     genders = new Array<GenderType>();

     loadGenders()
     {
        this.genders.push({name: "Male",isoCode: 1});
        this.genders.push({name: "FeMale",isoCode: 2});
     }

}

type GenderType = { name: string, isoCode: number };    // Specified format
like image 37
Mark Macneil Bikeio Avatar answered Oct 21 '22 07:10

Mark Macneil Bikeio