call()
var x = {
name:"t-io",
show:function(id){
console.log(this.name + id);
},
hide:function(id){
console.log(this.location.href);
console.log("隐藏" + id);
}
}
x.show.call(a, 1314); //将打印t-io1314
x.hide.call(window, 3344);
- call()的第一个参数是方法体中this所会指向的那个对象,如果为null则指向window
- call()第一个以后的参数依次对应原方法的参数
apply()
apply方法和call方法有些相似,但第二个参数必须是一个数组,其它原则参考call()
var x = {
name:"t-io",
show:function(id){
console.log(this.name + id);
},
hide:function(id, id2){
console.log(this.location.href);
console.log("隐藏" + id + id2);
}
}
x.show.apply(a, [1314]); //将打印t-io1314
x.hide.apply(window, [3344, 6688]);
bind()
看一个例子大家就能明白bind是个什么了
var log = console.log.bind(console);
log("666"); //等价于console.log("666");
t-io官网利用后端渲染技术和bind()方法,动态选择要不要打印日志,此思考大家可以参考,在生产环境尽量不打日志
<#if console.log == true >
var log = console.log.bind(console);
<#else>
var log = function () { };
</#if>
<#if console.info == true >
var info = console.info.bind(console);
<#else>
var info = function () { };
</#if>
<#if console.error == true >
var error = console.error.bind(console);
<#else>
var error = function () { };
</#if>