-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
87 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
Function.prototype.myApply = function (context, ...args) { | ||
context = Object(context); // 如果不是对象类型?则转为对象,以此保证 this 是个 对象 | ||
|
||
// context 创建一个 symbol 的key,并赋值 this | ||
const fn = Symbol('myApply'); | ||
context[fn] = this; | ||
// 然后 调用 context[fn]() | ||
let ret = undefined; | ||
|
||
if (args) { | ||
ret = context[fn](...args); | ||
} else { | ||
ret = context[fn](); | ||
} | ||
|
||
// 删除这个函数 | ||
delete context[fn]; | ||
// 返回结果 | ||
return ret; | ||
}; | ||
|
||
const fn = function (a, b) { | ||
console.info(a, b); | ||
}; | ||
|
||
fn.myApply(null, 1, 2); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Function.prototype.myBind = function (context, ...args) { | ||
const self = this; | ||
return (...innerArgs) => { | ||
// new 关键字 | ||
if (this instanceof self) { | ||
console.info('is new'); | ||
return new self(...args, ...innerArgs); | ||
} | ||
|
||
console.info('no new'); | ||
return self.call(context, ...args, ...innerArgs); | ||
}; | ||
}; | ||
const fn = function (a, b) { | ||
console.info(a, b); | ||
}; | ||
|
||
const fnBind = fn.myBind(null, 1, 2); | ||
|
||
fnBind(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Function.prototype.myCall = function (context, [...args]) { | ||
if (context === null || context === undefined) { | ||
context = this || window; | ||
} else { | ||
context = Object(context); // 如果不是对象类型?则转为对象,以此保证 this 是个 对象 | ||
} | ||
const fn = Symbol('myCall'); | ||
context[fn] = this; | ||
|
||
let ret = context[fn](...args); | ||
delete context[fn]; | ||
return ret; | ||
}; | ||
|
||
const fn = function (a, b) { | ||
console.info(a, b); | ||
}; | ||
|
||
const fnBind = fn.myCall(null, [1, 2]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters