apply方法:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script type="text/javascript">
var name = '乔丹'
var person = {
getName: function() {
return this.name
}
}
var person1 = {
name: '科比',
}
Function.prototype.myapply = function(context) {
if(typeof this !== "function") {
throw new Error('error')
}
context = context || window
context.fn = this
let result
if(arguments[1]) {
result = context.fn(...arguments[1])
} else {
result = context.fn()
}
delete context.fn
return result
}
console.log(person.getName.myapply(person1,[1,2,3]))
</script>
</body>
</html>
call方法:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script type="text/javascript">
var name = '乔丹'
var person = {
getName: function() {
return this.name
}
}
var person1 = {
name: '科比',
}
Function.prototype.myapply = function(context) {
if(typeof this !== "function") {
throw new Error('error')
}
context = context || window
context.fn = this
let result
if(arguments[1]) {
result = context.fn(...arguments[1])
} else {
result = context.fn()
}
delete context.fn
return result
}
console.log(person.getName.myapply(person1,[1,2,3]))
</script>
</body>
</html>
核心思想就是:apply与call都应该写在Function.prototype上,然后首先判断该方法的调用者this是不是一个函数,不是则抛出一个错误。是则根据call及apply函数的参数来满足对应的功能,如果传入某个this(对象),则使用这个对象作为context,否则使用window作为context。之后就是把该方法的调用者this(上文中为person.getName方法)挂载到person1这个对象身上,然后person1就有了和person一样的方法context.fn()。不过不能影响person1这个对象,使用完后及时删除这个方法即可。