zoukankan      html  css  js  c++  java
  • 【前端学习笔记】函数定义、函数调用、this

    函数定义的三种方式与特点:
    1.函数声明;(特点:会被前置;重复定义函数时,最后一次定义有效。)
    2.函数表达式;
    3.函数实例化;(特点:只能访问本地作用域与全局作用域!!!)

    /* 对象实例化定义函数的特点 */
     var person = {name:"刘德华", age:50};
     (function(){
       var person = {name:"刘德华", age:30};
       (function() {
         var person = {name:"刘德华", age:10};
         console.log(person.name+person.age+"岁");
       })()
     })();
     // -->输出10岁的刘德华,变量逐级向上回溯。
     
    var person = {name:"刘德华", age:50};
    (function(){
      var person = {name:"刘德华", age:30};
      var func = new Function("console.log(person.name+person.age+'岁');");
      func();
    })(); //-->如果new Function()里面有10岁的刘德华,则为10岁的,否则直接50岁的。
    

     函数调用:

     1.函数调用模式; add(1)
     2.方法调用模式; myNumber.add(1)
     3.构造函数调用模式; new Function(...)

    // /* 函数调用模式(this) */
    function add(i,j){
      console.log(this); // --> window
      console.log(arguments); //--> [1,2]
      (function(){
          console.log(this); // --> window
      })();
      var sum = i+j;
      return sum;
    }
    add(1,2);
    
    var myNumber = {
      value: 1,
      add: function(i){
      	console.log(this); // --> {value:1,add:function(){}}
        var helper = function(i){
            console.log(this); // --> window
              this.value += i; //所以这个实现不了;
        }
        helper(i);
      }
    }
    myNumber.add(1);
    

      

     

  • 相关阅读:
    @ModelAttribute 与@InitBinder
    springboot开启矩阵传参MatrixVariable
    socket(一)
    request请求《一》
    Ajax请求中的async:false/true的作用
    java.lang.NullPointerException at org.apache.jsp.index_jsp._jspInit(index_jsp.java:40)
    shiro登录源码
    js(正则验证)
    多进程之间的通信
    队列中常用方法的使用
  • 原文地址:https://www.cnblogs.com/zachary93/p/6057034.html
Copyright © 2011-2022 走看看