• javascript之函数


    1 函数

    1.1 函数语法

    /*  函数的一般形式和调用方法如下 */
    ---
    function 函数名([形参列表]){
        执行代码;
    };
    函数名([实参列表]);
    ---
    function add(number0,number1){
        var sum = number0 + number1;
        return sum;
    };
    var x = add(2,3);//5
    

    1.2 函数的定义

    /* 函数申明 */
    function add(number0,number1){
        var sum = number0 + number1;
        return sum;
    }
    
    /* 函数表达式 */
    var add = function(number0,number1){
        var sum = number0 + number1;
        return sum;
    }
    
    var x = add(3,4);//7
    

    1.3 函数的调用

    /* 函数名([实参列表]) */
    var add = function(number0,number1){
        var sum = number0 + number1;
        return sum;
    }
    
    var x = add(7,9);//16
    

    1.4 函数的参数

    /* 参数为原始类型:值传递 */
    function increment(number){
        number = number + 1;
        return number;
    };
    var a = 1;
    var x = increment(a);
    //x=2,但是a=1,没有变;
    
    /* 参数为对象类型:引用传递 */
    var ageIncrement = function(person){
        person.age = person.age + 1;
        return person;
    };
    var jerry = {name:"jerry",age:1};
    var x = ageIcrement(jerry);
    //x={name:"jerry",age:2},而且jerry也已经变成了jerry = {name:"jerry",age:2};
    
    /* 任意个数字的和 */
    var sum = function(){
        var length = arguments.length,
        sum = 0,
        parameter,
        for(var i = 0;i<length;i++){
            parameter = argument[i];
            sum += parameter;
        }
        return sum;//这里return一定在for循环之外的哦!!
    };
    add(1,3);//4
    add(3,4,8,9);//24
    

    1.5 作用域

    /* example one */
    var yaoming = {
        name:"姚明",
        gerder:1
    };
    function football(){
        var yaoming = {
            name:"要命",
            gender:0
        };
        yaoming.name = "耀名";
        yaoming.gender = 3;
    }
    football();
    yaoming;
    //{name:"姚明",gender:1},因为football()函数只把该函数内部的yaoming变为{name:"耀名",gender=3}
    
    /* example two */
    var yaoming = {
        name:"姚明",
        gender:1
    };
    function football(){
        yaoming.name = "耀名";
        yaoming.gender = 4;
    }
    football();
    yaoming;
    //{name:"耀名",gender:4},这里football()函数中作用的对象yaoming,只能是最前面的yaoming。
    

    1.6 作为对象的属性

    /* 函数作为对象的属性,使得对象有一定的行为 */
    var point = {
        x: 1,
        y: 1,
        move(stepX,stepY){
            point.x += stepX;
            point.y += stepY;
        }
    };
    point.move(2,1);
    //point.x = 3;point.y = 2;
    
    /* 对象属性函数中的对象本身可以用this来代替,上面的point对象可以改写如下 */
    var point = {
        x: 1,
        y: 1,
        move(stepX,stepY){
            this.x += stepX;
            this.y += stepY;
        }
    };
    point.move(2,1);
    //point.x = 3;point.y = 2;
    

    1.7 构造函数

    /* Point */
    function Point(x,y){
        this.x = x;
        this.y = y;
        this.move = function(stepX,stepY){
            this.x +=stepX;
            this.y +=stepY;
        }
    }
    
    var point = new Point(1,1);
    var point1 = new Point(2,2);
    var point2 = new Point(3,3);
    
    /* Car */
    function Car(type,color){
        this.type = type;
        this.color = color;
        this.status = "stop";
        this.light = "on";
        console.log(this);//Car{type:"benz",color:"red",status:"stop",light:"off"}
    }
    Car.prototype.start =function(){
        this.status = "driving";
        this.light = "on";
        console.log(`${this.type} is ${this.status},with light ${this.light}`);
    }
    Car.prototype.stop = function(){
        this.status = "stop";
        this.light = "off";
        console.log(`${this.type} is ${this.status},with light ${this.light}`);
    }
    var benz = new Car('benz','red');
    benz.start();//benz is driving with light on
    benz.stop();//benz is stop with light off
    

    1.8 原型

    /* 用原型的方法,为构造函数上添加属性 */
    function Point(x,y){
        this.x = x;
        this.y = y;
    }
    Pint.prototype.move = function(stepX,stepY){
        this.x += stepX;
        this.y += stepY;
    };
    var point = new Point(1,1);
    point.move(2,3);
    //point = {x:3,y:4}
    

    1.9 箭头函数 ()=>{}

    箭头函数中的this指向的是定义时的this,而不是执行时的this。

    var obj = {
        x:100,
        show(){
            setTimeout(function(){console.log(this.x);},500);
        };
    };
    obj.show();//undefined    setTimeout中this指的是windows
    
    var obj = {
        x:100,
        show() {
          setTimeout(()=>{console.log(this.x);},500);
        };
    };
    obj.show();//100   箭头函数中的this指向的是定义时的this,而不是执行时的this
    
  • 相关阅读:
    汇编语言-子程序调用
    汇编语言-转移指令的原理
    汇编语言-直接定址表
    汇编语言-内中断
    汇编语言-汇编程序初识
    【Mybtais】Mybatis 插件 Plugin开发(一)动态代理步步解析
    【Redis】redis异步消息队列+Spring自定义注解+AOP方式实现系统日志持久化
    【ECharts】报表联动,动态数据设计
    【】POST、GET、RequestParam、ReqestBody、FormData、request payLoad简单认知
    【TensorFlow】Win7下使用Object Detection API 训练自己的数据集,并视频实时检测
  • 原文地址:https://www.cnblogs.com/luwanlin/p/14323592.html
Copyright © 2020-2023  润新知