Skip to content

Commit

Permalink
完善new bind apply
Browse files Browse the repository at this point in the history
  • Loading branch information
abstain23 committed Jun 30, 2020
1 parent b68f300 commit 00a915e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
26 changes: 20 additions & 6 deletions 手写一个apply.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
Function.prototype.myApply = function(context, arr) {
function copySymbol(obj) {
const unique_proper = "00" + Math.random();
if (obj.hasOwnProperty(unique_proper)) {
arguments.callee(obj)//如果obj已经有了这个属性,递归调用,直到没有这个属性
} else {
return unique_proper;
}
}
const isArray = (obj) => Object.prototype.toString.call(obj).slice(8, -1).toLocaleLowerCase() === 'array'
Function.prototype.myApply = function (context, arr) {
if(typeof this !== 'function') {
return 'err'
}
context = context || window
context.fn = this
const fnName = copySymbol(context)
context[fnName] = this
let res
if(!arr) {
res = context.fn()
if (arr) {
if (!isArray(arr)) {
throw '第二个参数必须是数组'
}
res = context[fnName](...arr) //如果有返回值
} else {
res = context[fnName]()
}
res = context.fn(...arr) //如果有返回值
delete context.fn
delete context[fnName]
return res
}
28 changes: 27 additions & 1 deletion 手写实现new.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,30 @@ function copyNew(){
obj.__proto__ = Constructor.prototype
const res = Constructor.apply(obj,[...arguments].slice(1))
return res && (typeof res === 'object') ? res : obj
}
}


const isObject = (obj) => typeof obj === 'object' && obj !== null
const isFunction = (fn) => typeof fn === 'function'

function newOperator(Constructor, ...args) {
if (typeof Constructor !== 'function') {
throw '第一个参数必须是一个函数'
}
// newOperator.target = Constructor
const newObj = Object.create(Constructor.prototype)
const res = Constructor.call(newObj, ...args)
if (isObject(res) || isFunction(res)) {
return res
}
return newObj
}

/**
* new 做了什么
* 1. 创建一个新的对象
* 2. 这个对象的__proto__, 会指向构造函数的prototype
* 3. this指向这个新到对象
* 4. 执行构造函数中到代码
* 5. 如果该构造函数返回的是引用类型,那么会返回该引用类型,否则返回这个新的对象
*/

0 comments on commit 00a915e

Please sign in to comment.