You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
functionsum(x,y){console.log(x+y)}sum.call(null,2,4)// 6sum.apply(null,[2,4])// 6// --------------------------------------------------// 对字符串中的单个字符进行重复操作//Uncaught TypeError: "abcd".map is not a function'abcd'.map(i=>console.log(i))// aabbccddArray.prototype.map.call('abcd',i=>i+i).join('')
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bindif(!Function.prototype.bind){Function.prototype.bind=function(oThis){if(typeofthis!=='function'){// closest thing possible to the ECMAScript 5// internal IsCallable functionthrownewTypeError('Function.prototype.bind - what is trying to be bound is not callable');}varaArgs=Array.prototype.slice.call(arguments,1),fToBind=this,fNOP=function(){},fBound=function(){returnfToBind.apply(thisinstanceoffNOP
? this
: oThis,aArgs.concat(Array.prototype.slice.call(arguments)));};if(this.prototype){// Function.prototype doesn't have a prototype propertyfNOP.prototype=this.prototype;}fBound.prototype=newfNOP();returnfBound;};}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
含义(Meaning)
当
xOfVal
属于对象a
时,this
指向a
;当xOfVal
属于对象b
时,this
指向b
,因此打印出不同的值。由于this
的指向是可变的,所以可以手动切换运行环境,以达到某种特定的目的。结论:如果一个函数在全局环境中运行,
this
就是指向顶层对象(浏览器中为window
对象);如果一个函数作为某个对象的方法运行,this
就是指向那个对象。 可以近似认为,this
事所有函数运行时的一个隐藏参数,决定了函数的运行环境。使用场合(Using The Occasion)
全局环境(Global Context)
结论:
this
不论是不是在函数内部,只要是在全局环境下运行,this
就是指向全局对象window
构造函数(Constructor)
对象方法(Object Method)
a
对象的方法被赋予b
对象,该方法就变成了普通函数。其中的this
就从指向a
对象变成指向b
对象。这就是this
取决于运行时所在的对象的含义。需要特别小心。如果将某个对象的方法赋值给另一个对象,会改变this
的指向。this
的指向,可以将b.f
改写为箭头函数(Arrow functions)
结论:无论使用何种办法,
foo
方法的this
指向都是创建时的指向(window)。这个结论同样适用于在其他函数内部创建的箭头函数。this
指向创建它的对象。例:
对象定义新属性方法(Getter Or Setter)
Node.js
在node.js,
this
分成两种情况。在全局环境中,this
指向全局对象global
;在模块环境中,this
指向module.exports
使用注意事项(Note)
避免多层
this
避免数组处理方法中的
this
数组的
map
,reduce
,some
,filter
,forEach
等方法,允许提供一个函数作为参数。这个函数内部不应该使用this
。避免回调函数中的
this
固定
this
的方法(Fixed this)this
的动态切换,为JavaScript创造了巨大的灵活性,但也使编程变得困难和模糊。有时需要把this
固定下来,避免出现意想不到的情况。JavaScript提供了call
,apply
,bind
三个方法,来切换/固定this
的指向。call
方法call
的应用:调用对象的原生方法hasOwnProperty
是obj
对象继承的方法,如果这个方法一旦被覆盖,就得不到正确的结果。call
方法则可以解决这个问题。它将hasOwnProperty
方法的原始定义放到obj
对象上执行,这样无论obj
上有没有同名方法,都不会影响其结果。apply
方法应用(Use)
undefined
(数组遍历会跳过空元素,但是不会跳过undefined)length
属性,以及相对应的数字键)bind
方法bind
方法用于将函数体内的this
绑定到某个对象,然后返回一个新函数。bind
除了可以绑定this
以外,还可以绑定原函数的参数注意事项(Note)
bind
方法的兼容(Polyfill)call
方法使用bind
方法,将[1, 2, 3].slice(0, 1)
变成了slice([1, 2, 3], 0, 1)
的形式。这种改变也可以应用到其他数组方法。Function.prototype.call
绑定到Function.prototype.bind
对象,bind
的调用形式也可以被改写参考资料
Beta Was this translation helpful? Give feedback.
All reactions