1

I am completely new to ionic2/Angular2/Typescript. I am creating a wheel having eight slices. I have difficulty in declaration of variables. How do I declare multiple variables?

In javascript I have declared like below:

function rand(min, max) {
  return Math.random() * (max - min) + min;
}

    var color = ['#fbc','#f88','#fbc','#f88','#fbc','#f88', "#fbc", "#f67"];
    var label = ['10', '200', '50', '100', '5', '500', '0', "jPOT"];
    var slices = color.length;
    var sliceDeg = 360/slices;
    var deg = rand(0, 360);
    var speed = 0;
    var slowDownRand = 0;
    var ctx = canvas.getContext('2d');
    var width = canvas.width; // size
    var center = width/2;      // center
    var isStopped = false;
    var lock = false;

    function deg2rad(deg) {
      return deg * Math.PI/180;
    }

How do I declare in ionic2 ?

1
  • dont know about ionic but in typescript use can use var too but there is type let that you can use for local scope Commented Apr 12, 2017 at 10:02

2 Answers 2

1

Ionic 2 is running on top of Typescript.So you can do it as shown below.

Always try to use let.You can read why here.

rand(min, max): your-return-type {
  return Math.random() * (max - min) + min;
}

    let color = ['#fbc','#f88','#fbc','#f88','#fbc','#f88', "#fbc", "#f67"],
       label = ['10', '200', '50', '100', '5', '500', '0', "jPOT"],
       slices = color.length,
       sliceDeg = 360/slices,
       deg = rand(0, 360),
       speed = 0,
       slowDownRand = 0,
       ctx = canvas.getContext('2d'),
       width = canvas.width,
       center = width/2,
       isStopped = false,
       lock = false;

    deg2rad(deg): your-return-type2 {
      return deg * Math.PI/180;
    }
Sign up to request clarification or add additional context in comments.

2 Comments

you can also use the same let keyword to declare all of them like let color = ..., label = ..., slides = ..., ....
Thanks for the nice tip @sebaferreras
0

Why you want to declare so many variables? If there are so many variables, better make a class in TypeScript and then create an object in your class and then finally assign the values to the class properties.

    class Wheel{
    public slices: string;
    public speed: number;
....
    }

This way you can define all the properties and use them wherever you want to use.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.