bind能够实现所谓的柯里化(function currying),即先传一部分参数进去,使用的时候再传另一部分参数,这种场景下也只能使用bind。
functionBaseClass() {this.hello =function() {this.talk(); }this.talk =function() { document.write("I'm BaseClass"); } };functionMyClass() { BaseClass.call(this);this.talk =function() { document.write("I'm MyClass"); } }; MyClass.prototype=newMyClass();vara =newMyClass(); ...
var name = 'global name'; function getName() { var name = 'function name'; console.log('in function', this); console.log(this.name); } getName(); console.log('out function', this) 在浏览器控制台执行结果如下图所示: 我们可以看到在全局环境中直接调用函数getName()相当于window.getName...
function(){this.disabled=truewindow.setTimeout(function(){// 在这个普通函数里面,我们要this由原来的window 改成 btnthis.disabled=false}.bind(btn),2000)//bind(btn) 等价于 bind(this)}) 🔔总结
call, apply都属于Function.prototype的一个方法,它是JavaScript引擎内在实现的,因为属于Function.prototype,...
varobj={name:"张三",age:20};functionmethod(){console.log(this);//window}method(); this指向的是window,那么怎么让他指向当前对象obj呢? 方法1:使用call 代码语言:javascript 代码运行次数:0 运行 AI代码解释 varobj={name:"张三",age:20};functionmethod(a,b,c){console.log(this,a,b,c);//{nam...
1、对象的继承,一般的做法是复制:Object.extend prototype.js的实现方式是: Object.extend = function(destination, source) { for (property in source) { destination[property] = source[property]; } ret ...
其实是一个很简单的东西,认真看十分钟就从一脸懵B 到完全 理解! 先看明白下面: 例 1 obj.objAge; // 17 obj.myFun() // 小张年龄 undefined 例 2 shows() // 盲僧 比较一下这两者 this 的差别,第一个打印里面的 this 指向 obj,第二个全局声明的 shows(
Theapply()method accepts arguments in an array: Example constperson = { fullName:function(city, country) { returnthis.firstName+" "+this.lastName+","+ city +","+ country; } } constperson1 = { firstName:"John", lastName:"Doe" ...
var func = function(arg1, arg2) { }; 1. 2. 3. 就可以通过如下方式来调用: AI检测代码解析 func.call(this, arg1, arg2); func.apply(this, [arg1, arg2]) 1. 2. 其中this 是你想指定的上下文,他可以是任何一个 JavaScript 对象(JavaScript 中一切皆对象)。