zoukankan      html  css  js  c++  java
  • 第164天:js方法调用的四种模式

    js方法调用的四种模式

    1方法调用模式

     1 function Persion() {
     2     var name1 = "itcast",
     3     age1 = 19,
     4     show1 = function() {
     5         console.log(this.name);
     6     };
     7 
     8     return {
     9         age : age1,
    10         name : name1,
    11         show : show1
    12     };
    13 }
    14 
    15 var p = new Persion();
    16 p.show();  //在show方法中的this指向了p对象。

    2 函数调用模式

    1 function add( a, b) {
    2     this.result = a + b;
    3 }
    4 
    5 add( 3, 9 ); //此方法执行的时候,this指向了window
    6 
    7 console.log(result);   

    3构造器调用模式

     1 function Persion(){
     2     this.name = "123";
     3     this.age = 19;
     4     this.show = function(){
     5         console.log(this.name);
     6     };
     7 }
     8 
     9 var p = new Persion();
    10 p.show();//  在show方法中方法this,指向了p对象实例。

    4call apply调用模式

    1 function add(a,b){
    2     this.result = a + b;s           
    3 }
    4 
    5 var p  = {};        //定义一个空对象。
    6 add.call(p,3,4);    //在这个方法调用的时候,this指向了p
    7 console.log(p.result);
    8 
    9 //apply和call是一样的用法,只不过apply第二个参数用数组进行传递。

    变量提升:函数执行之前,会先将函数中所有的变量,挪到最前面去声明。

    函数名提升 script中脚本,在执行之前,会先把脚本中的所有的函数先进行编译解析,然后执行普通的js代码。

  • 相关阅读:
    移动端图片按比例裁剪
    bootstrap悬停下拉菜单显示
    videojs兼容ie8
    ie浏览器不支持多行隐藏显示省略号
    rem和px
    浏览器默认返回,页面刷新
    centos src compile gcc 7.3
    docker与gosu
    centos 安装 kernel
    docker proxy
  • 原文地址:https://www.cnblogs.com/le220/p/8400219.html
Copyright © 2011-2022 走看看