3 回答
TA贡献1963条经验 获得超6个赞
class
作为一种方法
var foo = {};foo.someMethod = function(){
alert(this);}作为A函数
var foo = function(){
alert(this);
}
foo();这可能就是你被绊倒的原因
var foo = {};foo.someMethod = function (){
var that=this;
function bar(){
alert(that);
}}use strictthisundefined).
作为一个建设者
function Foo(){
this.confusing = 'hell yeah';}var myObject = new Foo();应用方法
function foo(a,b){
alert(a);
alert(b);
alert(this);}var args = ['ah','be'];foo.apply('omg',args);TA贡献1886条经验 获得超2个赞
这是定义的行为吗?跨浏览器安全吗?
有什么理由可以解释为什么它是这样的.
this
如果 this在构造函数中使用,并使用 new关键词, this引用将要创建的对象。 this即使在公开的方法中也会继续意味着目标。 如果 this在其他任何地方使用,包括嵌套的 受保护
函数,它引用全局范围(在浏览器中是窗口对象)。
添加回答
举报
