美文网首页
java 有关this,apply使用

java 有关this,apply使用

作者: Raral | 来源:发表于2021-10-30 18:53 被阅读0次

js的call

  • js的this指向改变
var value = "全局的value";
    let tempObj = {
        value: "指定对象的value"
    }

    function test() {
        console.log(arguments); //类数组
        console.log(this);
        console.log("输出:" + this.value);
    }
    test(); //输出:全局的value
    console.dir(test)
    test.call(tempObj, 1, 2, 3); //输出:指定对象的value

  • 使用

var s = "abc";


function change(str,callback) {
    debugger;
    var upStr = str.toUpperCase();
    callback && callback(upStr)
}

change(s,function(res) {
    console.log(res);
})
Array.prototype.push.apply(arr1,arr2)

java 的 apply

package xuexi.heima.Function;

import xuexi.heima.FunctionalInterface.Interface01;

import java.util.function.Function;

public class Demo01 {
    public static void main(String[] args) {
        String s = "1234";
        change(s,(String str)->{
            return Integer.parseInt(str);
        });
    }
    public static void change(String s, Function<String,Integer> fun){
        Integer i = fun.apply(s);
        System.out.println(i);
    }
}

相关文章

网友评论

      本文标题:java 有关this,apply使用

      本文链接:https://www.haomeiwen.com/subject/vvzgaltx.html