Javascript 你没有';我不知道JS书籍-软装订
我现在正在读书。在这本书中,有一节是关于技术的。基本上,它是将函数绑定到特定范围/上下文的一种变体 书中: 如果有一种方法可以为默认绑定(不是全局的或未定义的)提供不同的默认值,同时仍然可以通过隐式绑定或显式绑定技术手动绑定该函数,那就太好了 一般来说,我了解该函数的功能,但不包括此部分:Javascript 你没有';我不知道JS书籍-软装订,javascript,Javascript,我现在正在读书。在这本书中,有一节是关于技术的。基本上,它是将函数绑定到特定范围/上下文的一种变体 书中: 如果有一种方法可以为默认绑定(不是全局的或未定义的)提供不同的默认值,同时仍然可以通过隐式绑定或显式绑定技术手动绑定该函数,那就太好了 一般来说,我了解该函数的功能,但不包括此部分: bound.prototype = Object.create( fn.prototype ); 使用这种“软绑定”技术时,我们为什么要设置原型?第页: 为什么能够覆盖硬绑定的新功能很有用 此行为的主要原因
bound.prototype = Object.create( fn.prototype );
使用这种“软绑定”技术时,我们为什么要设置原型?第页:
为什么能够覆盖硬绑定的新功能很有用
此行为的主要原因是创建一个函数(可以
与基本忽略的新对象(用于构造对象)一起使用
这是一个硬绑定,但它预设了函数的部分或全部
论据。bind(..)的一个功能是
在第一个参数之后传递。此绑定参数默认为标准参数
基础函数的参数(技术上称为“部分
应用程序”,它是“currying”的子集
我想软绑定要想获得同样的好处(提供一个传递了默认参数的构造函数),就必须同时设置原型,否则在将绑定函数用作构造函数时会丢失原型函数:
//softBind code removed
function Test(arg1,arg2){
this.arg1=arg1;
this.arg2=arg2;
}
Test.prototype.sayArgs=function(){
console.log('args:',this.arg1,this.arg2);
}
//the binding part doesn't do anything so can pass null
var BoundTest = Test.softBind(null, 'arg1');
var bt = new BoundTest('arg2');
bt.sayArgs();//=args:arg1 arg2
如果您不必向Function.prototype添加软绑定,但要以这种方式使用它,您可以使用普通绑定,原生绑定或MDN polyfill
请注意,使用polyfill时,不能将falsy参数作为第一个要绑定的参数传递,否则它将中断
//Using the MDN bind polyfill you can't pass any falsy
// value like null, undefined,"",0 and false
var BoundTest = Test.bind({}, 'arg1');
var bt = new BoundTest('arg2');
bt.sayArgs();//=args:arg1 arg2;
在本文中,给出的示例实际上是不正确的:
var bar = foo.bind( null, "p1" );
var baz = new bar( "p2" );
应该是:
var bar = foo.bind('anything non falsy' , "p1" );
[更新]
//removed if, so it'll set softbind every time
Function.prototype.softBind = function(obj) {
var fn = this,
curried = [].slice.call( arguments, 1 ),
bound = function bound() {
return fn.apply(
(!this ||
(typeof window !== "undefined" &&
this === window) ||
(typeof global !== "undefined" &&
this === global)
) ? obj : this,
curried.concat.apply( curried, arguments )
);
};
bound.prototype = Object.create( fn.prototype );
return bound;
};
function Test(){
//this has the right prototype and has
// the default arguments
console.log('this is:', this,'arguments',arguments);
this.name='test';
}
Test.prototype.something=22
var BoundTest = Test.softBind(null,'arg1','arg2');
var bt = new BoundTest();
//bt is same as new Test('arg1','arg2') would be
console.log('bt is:',bt);
//this.name did not affect window
console.log(window.name);//not test
console.log(bt instanceof Test);//true
从第页开始:
为什么能够覆盖硬绑定的新功能很有用
此行为的主要原因是创建一个函数(可以
与基本忽略的新对象(用于构造对象)一起使用
这是一个硬绑定,但它预设了函数的部分或全部
论据。bind(..)的一个功能是
在第一个参数之后传递。此绑定参数默认为标准参数
基础函数的参数(技术上称为“部分
应用程序”,它是“currying”的子集
我想软绑定要想获得同样的好处(提供一个传递了默认参数的构造函数),就必须同时设置原型,否则在将绑定函数用作构造函数时会丢失原型函数:
//softBind code removed
function Test(arg1,arg2){
this.arg1=arg1;
this.arg2=arg2;
}
Test.prototype.sayArgs=function(){
console.log('args:',this.arg1,this.arg2);
}
//the binding part doesn't do anything so can pass null
var BoundTest = Test.softBind(null, 'arg1');
var bt = new BoundTest('arg2');
bt.sayArgs();//=args:arg1 arg2
如果您不必向Function.prototype添加软绑定,但要以这种方式使用它,您可以使用普通绑定,原生绑定或MDN polyfill
请注意,使用polyfill时,不能将falsy参数作为第一个要绑定的参数传递,否则它将中断
//Using the MDN bind polyfill you can't pass any falsy
// value like null, undefined,"",0 and false
var BoundTest = Test.bind({}, 'arg1');
var bt = new BoundTest('arg2');
bt.sayArgs();//=args:arg1 arg2;
在本文中,给出的示例实际上是不正确的:
var bar = foo.bind( null, "p1" );
var baz = new bar( "p2" );
应该是:
var bar = foo.bind('anything non falsy' , "p1" );
[更新]
//removed if, so it'll set softbind every time
Function.prototype.softBind = function(obj) {
var fn = this,
curried = [].slice.call( arguments, 1 ),
bound = function bound() {
return fn.apply(
(!this ||
(typeof window !== "undefined" &&
this === window) ||
(typeof global !== "undefined" &&
this === global)
) ? obj : this,
curried.concat.apply( curried, arguments )
);
};
bound.prototype = Object.create( fn.prototype );
return bound;
};
function Test(){
//this has the right prototype and has
// the default arguments
console.log('this is:', this,'arguments',arguments);
this.name='test';
}
Test.prototype.something=22
var BoundTest = Test.softBind(null,'arg1','arg2');
var bt = new BoundTest();
//bt is same as new Test('arg1','arg2') would be
console.log('bt is:',bt);
//this.name did not affect window
console.log(window.name);//not test
console.log(bt instanceof Test);//true
从第页开始:
为什么能够覆盖硬绑定的新功能很有用
此行为的主要原因是创建一个函数(可以
与基本忽略的新对象(用于构造对象)一起使用
这是一个硬绑定,但它预设了函数的部分或全部
论据。bind(..)的一个功能是
在第一个参数之后传递。此绑定参数默认为标准参数
基础函数的参数(技术上称为“部分
应用程序”,它是“currying”的子集
我想软绑定要想获得同样的好处(提供一个传递了默认参数的构造函数),就必须同时设置原型,否则在将绑定函数用作构造函数时会丢失原型函数:
//softBind code removed
function Test(arg1,arg2){
this.arg1=arg1;
this.arg2=arg2;
}
Test.prototype.sayArgs=function(){
console.log('args:',this.arg1,this.arg2);
}
//the binding part doesn't do anything so can pass null
var BoundTest = Test.softBind(null, 'arg1');
var bt = new BoundTest('arg2');
bt.sayArgs();//=args:arg1 arg2
如果您不必向Function.prototype添加软绑定,但要以这种方式使用它,您可以使用普通绑定,原生绑定或MDN polyfill
请注意,使用polyfill时,不能将falsy参数作为第一个要绑定的参数传递,否则它将中断
//Using the MDN bind polyfill you can't pass any falsy
// value like null, undefined,"",0 and false
var BoundTest = Test.bind({}, 'arg1');
var bt = new BoundTest('arg2');
bt.sayArgs();//=args:arg1 arg2;
在本文中,给出的示例实际上是不正确的:
var bar = foo.bind( null, "p1" );
var baz = new bar( "p2" );
应该是:
var bar = foo.bind('anything non falsy' , "p1" );
[更新]
//removed if, so it'll set softbind every time
Function.prototype.softBind = function(obj) {
var fn = this,
curried = [].slice.call( arguments, 1 ),
bound = function bound() {
return fn.apply(
(!this ||
(typeof window !== "undefined" &&
this === window) ||
(typeof global !== "undefined" &&
this === global)
) ? obj : this,
curried.concat.apply( curried, arguments )
);
};
bound.prototype = Object.create( fn.prototype );
return bound;
};
function Test(){
//this has the right prototype and has
// the default arguments
console.log('this is:', this,'arguments',arguments);
this.name='test';
}
Test.prototype.something=22
var BoundTest = Test.softBind(null,'arg1','arg2');
var bt = new BoundTest();
//bt is same as new Test('arg1','arg2') would be
console.log('bt is:',bt);
//this.name did not affect window
console.log(window.name);//not test
console.log(bt instanceof Test);//true
从第页开始:
为什么能够覆盖硬绑定的新功能很有用
此行为的主要原因是创建一个函数(可以
与基本忽略的新对象(用于构造对象)一起使用
这是一个硬绑定,但它预设了函数的部分或全部
论据。bind(..)的一个功能是
在第一个参数之后传递。此绑定参数默认为标准参数
基础函数的参数(技术上称为“部分
应用程序”,它是“currying”的子集
我想软绑定要想获得同样的好处(提供一个传递了默认参数的构造函数),就必须同时设置原型,否则在将绑定函数用作构造函数时会丢失原型函数:
//softBind code removed
function Test(arg1,arg2){
this.arg1=arg1;
this.arg2=arg2;
}
Test.prototype.sayArgs=function(){
console.log('args:',this.arg1,this.arg2);
}
//the binding part doesn't do anything so can pass null
var BoundTest = Test.softBind(null, 'arg1');
var bt = new BoundTest('arg2');
bt.sayArgs();//=args:arg1 arg2
如果您不必向Function.prototype添加软绑定,但要以这种方式使用它,您可以使用普通绑定,原生绑定或MDN polyfill
请注意,使用polyfill时,不能将falsy参数作为第一个要绑定的参数传递,否则它将中断
//Using the MDN bind polyfill you can't pass any falsy
// value like null, undefined,"",0 and false
var BoundTest = Test.bind({}, 'arg1');
var bt = new BoundTest('arg2');
bt.sayArgs();//=args:arg1 arg2;
在本文中,给出的示例实际上是不正确的:
var bar = foo.bind( null, "p1" );
var baz = new bar( "p2" );
应该是:
var bar = foo.bind('anything non falsy' , "p1" );
[更新]
//removed if, so it'll set softbind every time
Function.prototype.softBind = function(obj) {
var fn = this,
curried = [].slice.call( arguments, 1 ),
bound = function bound() {
return fn.apply(
(!this ||
(typeof window !== "undefined" &&
this === window) ||
(typeof global !== "undefined" &&
this === global)
) ? obj : this,
curried.concat.apply( curried, arguments )
);
};
bound.prototype = Object.create( fn.prototype );
return bound;
};
function Test(){
//this has the right prototype and has
// the default arguments
console.log('this is:', this,'arguments',arguments);
this.name='test';
}
Test.prototype.something=22
var BoundTest = Test.softBind(null,'arg1','arg2');
var bt = new BoundTest();
//bt is same as new Test('arg1','arg2') would be
console.log('bt is:',bt);
//this.name did not affect window
console.log(window.name);//not test
console.log(bt instanceof Test);//true
为什么我们在使用这种“软绑定”技术时必须建立一个原型
这是为了完整,而不是别的。如果有人试图创建软绑定函数的新的
实例,他们希望生成的对象链接到与原始函数的.Prototype
指向的对象相同的[[Prototype]]
链接。因此,我们确保将bound.prototype
设置为等于引用同一对象
有人想打电话给<代码