zoukankan      html  css  js  c++  java
  • javascript基础拾遗(一)

    1.判断变量类型

        var num = '123';
        if(typeof num == 'number'){
            alert('this is a number');
        }
        else{
            throw 'this is not a number';
        }
    

    2.arguments关键字
    只在函数内部起作用,函数所有入参

    function foo(x) {
            console.log('x is :' + x);
            for(var i=0;i<arguments.length;i++){
                console.log('arguments['+i+']:'+arguments[i]);
            }
        }
        foo(1,2,3);
    

    3.rest关键字
    除了函数定义参数外的其他入参

    function foo(x,...rest) {
            console.log('x is :' + x);
            for(var i=0;i<arguments.length;i++){
                console.log('arguments['+i+']:'+arguments[i]);
            }
            console.log(rest)
        }
        foo(1,2,3);
    

    4.JavaScript会在行末自动添加分号

    function foo() {
            // right
            return {name: 'foo'}
            // wrong
            //return
            //{name:'foo'}
        }
        result = foo();
        console.log(result.name);
    

    5.变量作用域
    1)内部函数可以访问外部函数变量,外部变量不能访问内部函数的变量

    function foo() {
            x = 1;
            function calc() {
                // right
                y = x + 1;
            }
        }
        // wrong
        console.log(x);
    

    2)内部函数变量屏蔽外部变量

    function foo() {
            x = 1;
            function calc() {
                x = 2;
                // 2
                console.log(x);
            }
            // 1
            console.log(x);
        }
    

    3)变量提升
    javascript函数在执行时,会先扫描所有的变量定义,放在函数顶部

    function foo() {
            var x = 'hello ' + y;
            var y ='tomorrow';
            // 输出hello undefined
            console.log(x);
        }
        foo();
    

    可以看出,可以执行成功,y有申明,但是y要到下一条语句才会赋值。
    4)全局作用域
    不在任何函数内定义的变量具有全局作用域
    实际上,javascript有一个全局对象window,全局变量被绑定到这个全局对象window

    var x = 100
        // 100
        alert(window.x)
    

    window对象表示浏览器打开的当前窗口,拥有alert,length,height,location,screen等很多属性
    5)命名空间
    全局变量会绑定到window上,不同的javascript文件使用相同的全局变量会造成冲突。
    减少这种问题的一种方法是,把各自的变量和函数全部绑定到各自的全局变量,如:
    var MYAPP = {};
    MYAPP.name = 'myapp';
    MYAPP.version = 1.0;

    MYAPP.foo = function(){
    return 'foo';
    };
    6)局部作用域
    javascript的变量作用域针对的是函数,像for循环等语句块中的变量,属于块级作用域,是不具有局部作用域的。

    function foo() {
            for(var i=0;i<10;i++){
            
            }
            i = i +10;
            // i=20
            console.log(i)
        }
        foo();
    

    ES6引入了let关键字,使用let可以声明一个块级作用域的变量

    function foo() {
            for(let i=0;i<10;i++){
            
            }
            // wrong
            console.log(i)
        }
        foo();
    

    7)常量
    ES6引入了const关键字,来申明一个常量
    const PI =3.14
    8)解构赋值
    同时对一组变量进行赋值

    var array = ['hello', 'world']
    var a = array[0];
    var b = array[1];
    var [a,b] =  ['hello', 'world']
    

    8)异常处理

    try{
    }
    catch(e){
    }
    finally{
    }
    
  • 相关阅读:
    k-means算法
    偏差-方差分解Bias-Variance Decomposition
    常见machine learning模型实现
    Bag-of-words模型、TF-IDF模型
    atomic原子操作
    oc 计算 带括号 式子
    oc 基本语法 类 静态变量 常量
    通过文件头标识判断图片格式
    十大经典排序算法最强总结(含JAVA代码实现)(转)
    JPEG格式 介绍
  • 原文地址:https://www.cnblogs.com/shijingjing07/p/7988973.html
Copyright © 2011-2022 走看看