是否在Angular 2中指定了必要的类型和默认值?

时间:2017-02-07 21:35:08

标签: angular typescript

在Angular 2应用程序中,如果设置默认值,是否有必要(甚至是最佳实践)设置类型?

ex。)选项1

x <- list() 

# loop on tickers
for (i in 1:length(tickers)) {
    tmp <- get(tickers[i], pos=stockData)   # get data from stockData environment  
    colnames(tmp) <- c("Open", "High", "Low", "Close","Volume", "Adjusted")  # rename Header for all tables in list
    tmp$gl <-((Cl(tmp)-Op(tmp))/Op(tmp))*100   # Daily gain loss percentage
    SMA.n10 <- SMA(tmp[,4],n = 10)  # Calculate moving averages (MA) on "Close Price" <-column(4)
    BBands<- BBands(tmp[,2:4])
    tmp <- merge(tmp, SMA.n10, BBands)

    x[[i]]<-data.frame("time" = index(tmp), coredata(tmp), "Symbol" = tickers[i])  # merge data

}
x.df<- do.call(rbind, x) # call rbind to merge all xts objs in a single dataframe

或者这样:

ex。)选项2

import java.util.Scanner;  //importing scanner to get user input

public class ArrayHelper
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        int[] hello = new int[10];
        for(int i = 0; i < 10; i++) //to get right number of integers
        {
            System.out.print("Please enter an integer: ");
            hello[i] = input.nextInt();
        }
//printing everything out 
        display(hello);
        System.out.println();
        System.out.print("Evens: ");
        display(onlyEvens(hello));
        System.out.println();
        System.out.print("Positives: ");
        display(onlyPositives(hello));
        System.out.println();
        System.out.print("Odds: ");
        display(disjoint(hello ,onlyEvens(hello)));
        System.out.println();
        System.out.print("Negatives: ");
        display(disjoint(hello ,onlyPositives(hello)));
        System.out.println();
    }

    public static void display(int[] nums)
    {
        for(int i = 0; i < nums.length -1; i++)
            System.out.print(nums[i] + ", ");

        System.out.print(nums[nums.length - 1]); //!!!!!!this is where the error occurs
    }
    public static int[] onlyEvens(int[] nums) //only even numbers in array (anything divisible by 2 including 0)
    {
        int x = 0;  //for set length
        for(int i = 0; i < nums.length; i++)
            if (nums[i]%2 == 0) //checks if even
                x++;

        int[] y = new int[x];
        int z = 0;

        for(int i = 0; i < nums.length; i++)
            if (nums[i]%2 == 0) //checks if even
            {
                y[z] = nums[i];
                z++;
            }
        return y;
    }
    public static int[] onlyPositives(int[] nums) //looking for only positive integers in array
    {
        int x = 0;  //sets set length
        for(int i = 0; i < nums.length; i++)
            if (nums[i] > -1)   //checks if positive
                x++;

        int[] y = new int[x];
        int z = 0;

        for(int i = 0; i < nums.length; i++)
            if (nums[i] > -1)   //checks if positive
            {
                y[z] = nums[i];
                z++;
            }

        return y;
    }

    public static int[] disjoint(int[] nums, int[] nums2)
    {
        int x = 0;

        for(int i = 0; i < nums.length; i++)
        {
            int j = nums[i];
            if(!contains(nums2 , j))    //checks if letter be there
                x++;
        }

        for(int i = 0; i < nums2.length; i++)
        {
            int j = nums2[i];   //checks if letter be there
            if(!contains(nums , j))
                x++;
        }

        int[] y = new int[x];
        int z = 0;

        for(int i = 0; i < nums.length; i++)
        {
            int j = nums[i];    //checks if letter be there
            if(!contains(nums2 , j))
            {
                y[z] = nums[i];
                z++;
            }
        }

        for(int i = 0; i < nums2.length; i++)
        {
            int j = nums2[i];   //checks if letter be there
            if(!contains(nums , j))
            {
                y[z] = nums2[i];
                z++;
            }
        }
        return y;
    }
}

在上面的代码中,这是指定类型默认值的最佳做法?选项1或选项2?

2 个答案:

答案 0 :(得分:2)

类型是编译所必需的。实际值可以在运行时更改。如果指定变量的类型并且如果在编译时不知道类型,则最好的方法是使用any类型。在the language guide中对TypeScript语言进行了详细描述。

变量可以像代码,构造函数或方法/函数一样初始化 inline 。如果变量为undefined,则应在运行时确定默认值。

答案 1 :(得分:1)

Typescript将使用type inference猜测类型。我个人更喜欢为变量和属性指定类型,但是使用常量推断。