bind能够实现所谓的柯里化(function currying),即先传一部分参数进去,使用的时候再传另一部分参数,这种场景下也只能使用bind。
display:function() { letx = document.getElementById("demo"); x.innerHTML=this.firstName+" "+this.lastName; } } setTimeout(person.display,3000); Try it Yourself » Thebind()method solves this problem. In the following example, thebind()method is used to bind person.display to person...
pp.bind('click', opt,function(e) { Core.createPageNC(e.data); }); }
function.bind(thisArg[,arg1[,arg2[,argN]]]) 1. 传入的第一个参数被赋值给this 1. thisArg :The value to be passed as the this parameter to the target function when the bound function is called. The value is ignored if the bound function is constructed using the new operator.如果使用new(...
I thought I know the Function definition, execution context and the behavior of this in JavaScript. However, I realized that actually I don't or th...
function.bind(thisArg[,arg1[,arg2[,...]]])thisArg:要绑定到函数的对象,即绑定后函数的上下文。 arg1,arg2,...:可选参数,要传递给原始函数的参数。 bind() 方法返回一个新的绑定函数,它与原始函数具有相同的函数体和作用域,但具有固定的上下文。
console.log('in function', this); console.log(this.name); } getName(); console.log('out function', this) 在浏览器控制台执行结果如下图所示: 我们可以看到在全局环境中直接调用函数getName()相当于window.getName(),调用它的是全局对象window,所以this对象指向全局对象,也就是window对象。
function Point(x, y) { this.x = x; this.y = y; } Point.prototype.toString = function() { return this.x + ',' + this.y; }; var p = new Point(1, 2); p.toString(); // '1,2' // not supported in the polyfill below, // works fine with native bind: var YAxisPoint =...
js原生函数bind /*在javascript中,函数总是在一个特殊的上下文执行(称为执行上下文),如果你将一个对象的函数赋值给另外一个变量的话,这个函数的执行上下文就变为这个变量的上下文了。下面的一个例子能很好的说明这个问题 代码如下:*/window.name= "the window object"functionscopeTest() {returnthis.name;...
其实是一个很简单的东西,认真看十分钟就从一脸懵B 到完全 理解! 先看明白下面: 例 1 obj.objAge; // 17 obj.myFun() // 小张年龄 undefined 例 2 shows() // 盲僧 比较一下这两者 this 的差别,第一个打印里面的 this 指向 obj,第二个全局声明的 shows(