Skip to content

Commit

Permalink
chore: update apply、bind、call
Browse files Browse the repository at this point in the history
  • Loading branch information
veaba committed Jan 1, 2025
1 parent c1f2c52 commit ae59ba9
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 25 deletions.
26 changes: 26 additions & 0 deletions demos/js/apply-bind-call/apply.js
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);
20 changes: 20 additions & 0 deletions demos/js/apply-bind-call/bind.js
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();
19 changes: 19 additions & 0 deletions demos/js/apply-bind-call/call.js
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]);
47 changes: 22 additions & 25 deletions demos/js/call_apply_bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,35 @@

// 这是一个普通的函数
const call_apply_bind = function () {
console.info('Hello world:call_apply_bind ');
console.info(this);
console.info('Hello world:call_apply_bind ');
console.info(this);
};

// 这是一个箭头函数
const call_apply_bind_arrow = () => {
console.info('Hello world:call_apply_bind_arrow ');
console.info(this);
console.info('Hello world:call_apply_bind_arrow ');
console.info(this);
};

// arg 测试

const call_apply_bind_arg = function () {
console.info("call_apply_bind_arg:", arguments);
console.info('call_apply_bind_arg:', arguments);
};
const call_apply_bind_arg_arrow = () => {
console.info("call_apply_bind_arg_arrow:", arguments);
console.info('call_apply_bind_arg_arrow:', arguments);
};

const catObj = {
name: "小叮当",
age: 2,
sex: 'female'
name: '小叮当',
age: 2,
sex: 'female',
};

const dogObj = {
name: "二哈",
age: 1.5,
sex: "male"
name: '二哈',
age: 1.5,
sex: 'male',
};

// 直接指向函数
Expand Down Expand Up @@ -140,7 +140,6 @@ const dogObj = {

// 结论1,call可以改变入参的那个对象的内部结构


// apply的应用

// apply - 对数组进行取最大值
Expand Down Expand Up @@ -169,7 +168,6 @@ const dogObj = {
// console.info('list2:', list2);//[3,4]
// console.info('list3:', list3);//2


// 使用apply 链接构造器

// 方法1
Expand Down Expand Up @@ -200,7 +198,6 @@ const dogObj = {
// const res1 = add.call(null, 5, 3);
// console.info(res1);


// 实现类似bind的方法
// 要点1、返回一个返回
// apply的衍生实现也是需要调用apply、call?!
Expand Down Expand Up @@ -247,19 +244,19 @@ const dogObj = {
// 手写call

Function.prototype.theCall = function (ctx, ...arr) {
if (ctx === null || ctx === undefined) {
ctx = this;//自动指向
} else ctx = Object(ctx);//原始值的this 指向该原始值的实例对象
const tempPrototype = Symbol('hahh');
ctx[tempPrototype] = this;
let result = ctx[tempPrototype](...arr);
delete ctx[tempPrototype];
return result;
if (ctx === null || ctx === undefined) {
ctx = this; // 自动指向
} else ctx = Object(ctx); // 原始值的this 指向该原始值的实例对象

const tempPrototype = Symbol('hahh');
ctx[tempPrototype] = this;
let result = ctx[tempPrototype](...arr);
delete ctx[tempPrototype];
return result;
};

const x = function (a, b) {
console.info(a, b);
console.info(a, b);
};

x.theCall(null, 999, 666);
Expand Down

0 comments on commit ae59ba9

Please sign in to comment.