zoukankan      html  css  js  c++  java
  • typescript变量声明(学习笔记非干货)

    var a=10;
    function f(){
        var message="hello,world";
        return message;
    }
    function f(){
        a=10;
        return function g(){
            var b=a+1;
            return b;
        }
    }
    var g=f();
    g();//11
    

    var 声明有一些奇怪的作用域规则
    The var declaration has some strange scoping rules

    function f(shouldinitalize:boolean){
        if(shouldinitalize){
            var x=10;
        }
        return x;
    }
    f(true)//'10'
    f(false)//undefined
    
    

    多次声明同一个变量并不会报错
    Repeated declarations of the same variable do not cause errors

    function sumMatrix(matrix:number[][]){
        var sum=0;
        for(var i=0;i<matrix.length;i++){
            var currentRow=matrix[i];
            for(var i=0;i<currentRow.length;i++){
                sum+=currentRow[i];
            }
        }
        return sum;
    }
    
    for(var i=0;i<10;i++){
        setTimeout(function(){console.log(i);},100*i);
    }
    for(var i=0;i<10;i++){
        (function(i){
            setTimeout(function(){console.log(i);},100*i)
        })(i);
    }
    function f(input:boolean){
        let a=100;
        if(input){
            let b=a+1;
            return b;
        }
        return b;
    }
    

    catch语句声明的变量也具有同样的作用域规则
    Variables declared by catch statements also have the same scoping rules

    try{
        throw "wowow"
    }catch(e){
        console.log("oh well")
    }
    console.log(e);
    

    重定义及屏蔽
    Redefinition and Shielding

    function f(x){
        var x;
        var x;
        if(true){
            var x;
        }
    }
    

    块级作用域需要在不用的块里声明
    Block-level scopes need to be declared in unused blocks

    function f(condition,x){
        if(condition){
            let x=100;
            return x;
        }
        return x;
    }
    f(false,0);//return 0
    f(true,0)//return 100
    

    在一个嵌套作用域里引一个新的名字称作屏蔽
    Lead a new name in a nested scope called shielding

    function sumMatrix(matrix:number[][]){
        let sum=0;
        for(let i=0;i<matrix.length;i++){
            var currentRow=matrix[i];
            for(let i=0;i<currentRow.length;i++){
                sum+=currentRow[i];
            }
        }
        return sum;
    }
    

    块级作用域变量的获取
    Obtaining Block-level Scope Variables

    function theCityThatAlwaysSleeps(){
        let getCity;
        if(true){
            let city="Seattle";
            getCity=function(){
                return city;
            }
        }
        return getCity();
    }
    for(let i=0;i<10;i++){
        setTimeout(function(){console.log(i);},100*i);
    }
    

    const与let声明类似,他们被赋值后不能再改变,他们拥有与let相同作用域规则,凡事不能对他们重新赋值
    Like let declarations, const cannot be changed after they are assigned. They have the same scoping
    rules as let, and nothing can be reassigned to them.

    const numLivesForCat=9;
    const kitty={
        name:"Aurora",
        numLives:numLivesForCat,
    }
    //Error
    kitty={
        name:"Danielle",
        numLives:numLivesForCat
    }
    //all okay
    kitty.name="Rory";
    kitty.name="Kitty";
    kitty.name="Cat";
    kitty.numLives--;
    

    最简单的解构莫过于数组的解构赋值
    The simplest deconstruction is the deconstruction assignment of arrays

    let input=[1,2];
    let [first,second]=input;
    console.log(first);
    console.log(second);
    

    解构用于已经声明的变量就更好
    It's better to deconstruct variables that have been declared

    [first,second]=[second,first];
    

    作用域函数参数
    Scope function parameters

    function f([first,second]:[number,number]){
        console.log(first);
        console.log(second);
    }
    f(input);
    

    使用...语法创建一个剩余变量列表
    Create a list of remaining variables using the... Grammar

    let[first,...rest]=[1,2,3,4];
    console.log(first);
    console.log(rest);
    //
    let [first]=[1,2,3,4];
    console.log(first);//1
    let[,second,,fourth]=[1,2,3,4];
    

    对象解构
    Object deconstruction

    let o={
        a:"foo",
        b:12,
        c:"bar"
    }
    let {a,b}=o;
    

    就像数组解构,可以用没有声明的赋值
    Like array deconstruction, assignments without declarations can be used

    ({a,b}={a:"baz",b:101});
    

    属性命名
    Attribute naming

    let {a:newName1,b:newName2}=o;
    let newName1=o.a;
    let newName2=o.b;
    

    属性指示类型
    Attribute Indicator Type

    let{a,b}:{a:string,b:number}=0;
    

    默认值可以让你的属性为undefined时使用缺省值
    Default values allow you to use default values when your attribute is undefined

    function keepWholeObject(wholeObject:{a:string,b?:number}){
        let {a,b=1001}=wholeObject;
    }
    

    函数声明Function declaration

    type C={a:string,b?:number}
    function f({a,b}:C):void{
    
    }
    

    首先你需要知道在默认值之前设置类型
    First you need to know what type to set before the default value

    function f({a,b}={a:"",b:0}):void{
        //...
    }
    f()
    你需要知道在解构属性上给予一个默认或可选的属性来替换主初始化列表
    You need to know that the main initialization list is replaced by a default 
    or optional attribute on the deconstruction attribute.
    function f({a,b=0}={a:""}):void{
    
    }
    f({a:"yes"})
    f()
    f({})
    
  • 相关阅读:
    这可能是全网最全的流媒体知识科普文章
    JavaCV实战专栏文章目录(JavaCV速查手册)
    JavaCV复杂滤镜filter特效处理入门教程和常用案例汇总
    【PC桌面软件的末日,手机移动端App称王】写在windows11支持安卓,macOS支持ios,龙芯支持x86和arm指令翻译
    如何在龙芯架构和国产化操作系统平台上运行javacv
    如何在国产龙芯架构平台上运行c/c++、java、nodejs等编程语言
    JavaCV开发详解之35:使用filter滤镜实现画中画,以屏幕画面叠加摄像头画面为例
    JavaCV开发详解之34:使用filter滤镜实现字符滚动和无限循环滚动字符叠加,跑马灯特效制作
    JavaCV开发详解之33:使用filter滤镜实现动态日期时间叠加
    JavaCV开发详解之32:使用filter滤镜实现中文字符叠加
  • 原文地址:https://www.cnblogs.com/smart-girl/p/10338064.html
Copyright © 2011-2022 走看看