call()
image.png
applay()
image.png
bind()
image.png
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>单利模式应用</title>
</head>
<body>
</body>
</html>
<script>
//call
function stdunt(n1, n2) {
console.log(this.name) //this=>objs
console.log(this.name + n1 + n2) //this=>8
}
let objs = {
name: 3
}
stdunt.call(objs, 2, 3)
//applay
function hello(name, age) {
console.log(this)
console.log(name);
console.log(age + this.name);
console.log(arguments)
}
let util = {
name: 234
}
hello.apply(util, ["tsrot", 24, 12, 45, 6]);
//bind
var person = {
name: "tsrot",
age: 24,
sayHello: function (age) {
console.log(this.name);
console.log(age);
}
};
var son = {
name: "xieliqun"
};
var boundFunc = person.sayHello.bind(son);
boundFunc(25)
</script>
网友评论