箭头函数因为自己绑定了this,也就是运行时所在的对象,
所以不能使用call,apply,bind方法改变this的指向.
所以下面这段Template.bind({})
的作用是其实是复制函数,但是在箭头函数上绑定this是没有作用的
//👇 We create a “template” of how args map to rendering
const Template: ComponentStory<typeof Button> = (args) => <Button {...args} />;
//👇 Each story then reuses that template
export const Primary = Template.bind({});
Primary.args = { backgroundColor: '#ff0', label: 'Button' };
1.这个方法不会复制原函数的属性和原型链上的东西
2.新函数的this变量会被绑定在传入的变量上,即使你用apply传别的变量调用也不会改变
function oldFunc() {
console.log(this.msg);
}
var newFunc = oldFunc.bind({ msg: "You shall not pass!" }); // this object is binded
newFunc.apply({ msg: "hello world" }); //logs "You shall not pass!" instead
instanceofo判断bind复制的函数为true
(new newFunc()) instanceof oldFunc; //gives true
(new oldFunc()) instanceof newFunc; //gives true as well
newFunc == oldFunc; //gives false however
网友评论