zoukankan      html  css  js  c++  java
  • this对象

    1、改变this

    A:call()方法:

    function sayNameForAll(label){
        console.info(label + ":" + this.name);
    }
    
    var person1 = {
        name : "Nicholas"
    };
    
    var person2 = {
        name = "Greg"
    };
    
    var name = "Michale";
    
    sayNameForAll.call(this,"global");       //outputs “global:Michale”
    sayNameForAll.call(person1,"person1");   //outputs "person1:Nicholas"
    sayNameForAll.call(person2,"person2");   //outputs    "person2:Greg"

    B:apply()方法:和call()方法一样,接受两个参数,this的值和一个数组或者类似数组的对象,内含需要被传入函数的参数,不需要像使用call()那样一个个指定参数

    function sayNameForAll(label){
        console.info(label + ":" + this.name);
    }
    
    var person1 = {
        name : "Nicholas"
    };
    
    var person2 = {
        name = "Greg"
    };
    
    var name = "Michale";
    
    sayNameForAll.apply(this,["global"]);       //outputs “global:Michale”
    sayNameForAll.apply(person1,["person1"]);   //outputs "person1:Nicholas"
    sayNameForAll.apply(person2,["person2"]);   //outputs    "person2:Greg"
  • 相关阅读:
    CCF第四题无向图打印路径 欧拉问题
    20160330训练赛
    POJ 3281 网络流dinic算法
    校赛E题递归形式
    Mbx1996
    good luck
    hdu1054 树形dp&&二分图
    补上题代码 hdu1520
    hdu1520 树形dp Anniversary party
    2018 Multi-University Training Contest 2
  • 原文地址:https://www.cnblogs.com/zhanghuiyun/p/5658323.html
Copyright © 2011-2022 走看看